Re: Validation functions
| From: | Michael Dransfield | Date: | Mon, 24 Jun 2002 13:28:08 +0000 |
| Subject: | Re: Validation functions | ||
| References: | 1 2 3 4 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-7324@lists.php.net to get a copy of this message | ||
At 14:21 24/06/2002 +0200, you wrote:
Pierre-Alain Joye wrote:Im sorry I posted code with a pear error being returned... I just thought it was the right thing to do (now i realise that it isnt). I really wanted to discuss the idea of having international methods implemented so we can easilly check ANY postcode. Here is the rewritten function (without controversial error messages ;) function zipcode($zip, $cc){Do you want Validate return/raise a pear error on a non valid data ?
$cf = "Validate/Validate/$cc.php"; //// <-- probably change this
if (@require_once($cf)){
$vn = "Validate_$cc";
$vo = new $vn();
$zvf = $vo->getZipValFunc();
if (method_exists($vo, $zvf)){
return $vo->$zvf($zip);
}
else {
trigger_error("Cannot find zipcode function in $cc validation class ", E_USER_WARNING);
}
}
else {
trigger_error("Cannot find Validation file for $cc validation ($cf) ", E_USER_WARNING);
}
}
I have uploaded my copies of the Validate classes and a simple test here...
http://www.blueroot.net/pear/Validate_UK.phps
http://www.blueroot.net/pear/Validate_UK_example.phps
the postcode checking uses the international function above.
Another point you should not forget are complex valdations functions. Ie. i've wrote a validation function for checking german bank account numbers (but never finished). This functions requires two parameters: The bank code number and the account number. In germany there exists at the moment 100 different checksum routines. So the function first looks up in a database which routine to use. Then the function calls the routine and checks the number.I have written a similar function for UK bank accounts, but i have 2 seperate methods, one for bank code and one for a/c number. I wouldnt know where to begin to get the data required to do your sort of advanced checking (although it is possible here). This kind of checking would be implemented in a seperate function, which individually verified each part as valid then compares them.
In this situation you have three states: - the account number is correctreturn true
- the account number is not correctreturn false
- the account number can not be checked, because db look up fails or the account number needs to be checked by a new checksum routine, which isn't implemented yet (really possible) What to do now? Returning 'false' in the last case is'nt really usable, because the number may be correct.return true, you should give them the benefit of the doubt, maybe you can set a constant which flags up the record as needing to be manually checked. - and hurry up with those routines!! ;) I would prefer keeping basic checking functions as simple as possible and then create another class which can do complicated verification routines. For example i have a driving licence function which checks the dl number is valid. Here you can check a persons name and date of birth against their dl number. You can check postcodes against a city to verify that city is in that postcode. I havent put this functionality into the Validate_UK class, because i felt that there deserved to be an extended class which can deal with verification and information extraction (eg getting bank name from bank code)
And at the end, don't forget usability. As a user of your application, i would intrested in what is wrong, not only that something is wrong. A validation method which only returns true/false doesn't give the required informationhow far should the errors go? 'The credit card number you have entered is invalid, if you changed the expiry date to 05/03 - it would be valid' - sometimes the application developer may need extended information, but (web) users rarely need / should be told about the exact reason for their non valid data (guidance can be added by you)
-- PEAR Development Mailing List (http://pear.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php