Re: your method for validating forms

From: Date: Thu, 02 Oct 2003 02:16:16 +0000
Subject: Re: your method for validating forms
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-164654@lists.php.net to get a copy of this message
* Thus wrote Chris W. Parker (cparker@swatgear.com): > Hey people. > > > function validateFormData($input) > { > foreach($input as $field) > { > switch ($field['type']) > { > case 'name': > // do the magic > break; > case 'phonenumber': > // do the magic > break; > } > } > } > > > The validateFormData() function will return false if there are no errors > to report. I usually return an empty array. > > For a form that has a lot of fields to validate I think this can make > things pretty easy. > > > So... what do you think? I do this very exact thing in a lot of my code. The validation function's are usually related to the object I'm validating. function customer_info_validate($customer); Then you can take it a step further and make some generic common validation's like email, phone numbers, etc and your magic code would just call those. Then make a generic function that accepts a value and a regex pattern to validate against the value. So now your switch would be something like: case 'name': validate_regex($field['value'], '/^[a-z ]{5,50}$/i'); break; case 'us_phone': validate_us_phone($field['value']); break; etc.. Curt -- "I used to think I was indecisive, but now I'm not so sure."

« previous php.general (#164654) next »