HomePHP Page 7 - Building An Extensible Form Validator Class
Floating Like A Butterfly - PHP
Wondering what OOP can do for you? Well, wonder no more - thisarticle demonstrates how OOP can save you time and effort by building aPHP-based Form Validator object to validate HTML form input. In additionto a detailed walkthrough of the process of constructing a PHP class totest user input, this article also includes usage examples and a brieflook at some powerful open-source alternatives.
Two useful corollaries of the isNumber() method are the isInteger() and isFloat() methods, which provide for more precise data validation capabilities when dealing with numeric data.
Another very useful method, and one which I find very useful
when building forms which ask for the user's age, is the isWithinRange() method - it provides an easy way to check whether the input is within a certain numeric range.
<?php
class FormValidator
{
// snip
// check whether input is within a valid numeric range
function isWithinRange($field, $msg, $min, $max)
{
$value = $this->_getValue($field);
if(!is_numeric($value) || $value < $min || $value >
$max)
{
$this->_errorList[] = array("field" => $field,
"value" => $value, "msg" => $msg);
return false;
}
else
{
return true;
}
}
}
?>