What I'd like to do is get rid of all the if's and what not and throw them into a function. What I thought of doing to replace all this is:
<? // pseudo code
$formdata['fname']['data'] = (!empty($_GET['fname'])) ? $_GET['fname'] : "" ; $formdata['fname']['type'] = "name";
$_SESSION['form_errors'] = validateFormData($formdata);
redirect("to_the_previous_page.php");
?>
I use a validation class that's kind of like this. It has a main "check" method:
function check($type,$key,$allow_empty=0,$reqlength=0)
where you pass the "type" of validation you want to do (number, string, email, website, etc), the $_REQUEST array "key" that holds the value to be validated, a flag to allow empty values, and a flag to set a required length for numeric validations.
Validation ends up looking something like this:
$input['name'] = $val->check('string','name'); $input['email'] = $val->check('email','email'); $input['phone'] = $val->check('phone','phone',1); $input['company_name'] = $val->check('string','company_name'); $input['job_description'] = $val->check('ml_string','job_description'); $input['website'] = $val->check('website','website',1); $input['expiration'] = $val->check('number','expiration');
if($val->isErrors() { //display errors } else { //process "safe" values within $input }
Each "type" of validation is a different method within the class that has it's own rules. There are also methods to validate entries against a configuration arrays and methods to turn on and off things like htmlentities() of returned values.
If there is an error, the class makes a $css array entry with a key matching what was supposed to be validated. So for the above, if the "name" validation failed, a $css['name'] variable would be created with the value of an error CSS class. This is easily assigned to smarty to "mark" parts of the form to highlight where the errors were at:
<div class="{$css.name}">Name:</div>
The default will be a normal class and when there is an error, it'll get an "error" class that makes it bold, red, whatever.
A simple $smarty->assign_by_ref('css',$val->css); is all that's needed. :)
-- ---John Holmes...
Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/
php|architect: The Magazine for PHP Professionals – www.phparch.com
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php