Building An Extensible Form Validator Class - How Things Work (
Page 4 of 12 )
Now, before proceeding further, I
need to decide how this class is going to work. Here's how I plan to use it:
<?php
// some form processor
// let's assume that the variables $a and $b
// have been obtained via a form POST operation
// create an object
$fv = new FormValidator();
// run validation methods
// the first argument is the name of the field to validate
// the second is the error to report if validation fails
$fv->isString("a", "Please enter a string for field A");
// same here
$fv->isNumber("b", "Please enter a string for field B");
// check to see error status
echo $fv->isError();
?>
As you can see, I would like to call class methods with two
parameters: the name of the form variable to validate, and the error message to
return in the event the validation test fails. With this in mind, it becomes
much easier to design the class appropriately.
Once the basic
functionality of the class is clear, it's a good idea to spend some time listing
the important methods, together with their purpose. Here's my initial
cut:
isEmpty($var, $errormsg) - check whether the specified form variable
is empty;
isString($var, $errormsg) - check whether the specified form
variable contains string data;
isNumber($var, $errormsg) - check whether
the specified form variable contains numeric data;
isWithinRange($var,
$errormsg) - check whether the specified form variable contains a value within
the specified numeric range;
isEmailAddress($var, $errormsg) - check
whether the specified form variable contains a valid email
address;
isError() - check whether any errors have occurred in form
validation;
getErrorList() - return the list of validation
errors;
resetErrorList() - reset the error list.
These are the
essential methods; there may be more, which I will add as development
progresses.