D
Deleted member 43669
Guest
Most web frameworks I've seen handle form validation using exceptions; if the form fields or the whole form don't meet the requirements, an exception is thrown and dealt with (redirect back to the form with errors displayed).
Is this the right way to do it? It seems to me that this should be control flow, and exceptions are lousy for control flow. It should look something like
Thoughts?
Is this the right way to do it? It seems to me that this should be control flow, and exceptions are lousy for control flow. It should look something like
Code:
errors validate_form(values) {
errors = []
if(!is_number(values.age)) {
errors += MustBeNumeric(age);
}
if(!values.foo or !values.bar) {
errors += FooOrBarMustBePresent();
}
return(errors);
}
...
// framework code
handleForm() {
errors = form.validate_form(values);
if(errors) {
redirect_to_form(errors);
}
redirect_to_success();
}
Thoughts?