I followed the tutorial at http://www.devshed.com/c/a/PHP/Building-An-Extensible-Form-Validator-Class/ because I wanted to have an easier way to build and maintain form validation.
I completed the tutorial, but the form does not register the form as being filled out.
Here is the class code (FormValidator.class.inc)
<?php
//FormValidator.class.inc
//class to perform form validation
class FormValidator {
//---------------private variables------------------
var $_errorList;
//the _ prefix is used to distinguish private variables from public variables
//---------------methods (private)------------------
//functio to get the value of a variable (field)
function _getValue($field) {
global ${$field}; //makes the field entry a global variable
return ${$field};
}
//---------------methods (public)-------------------
//it is a good idea to run resetErrorList() whenever the class is first initialized
//PHP makes it possible to automatically execute a specific function when a new instance of a class is spawned. This function is referred to as a "constructor" and must have the same name as the class.
//reset error list
function FormValidator() {
$this->resetErrorList();
}
//begin basic validation routines
//check whether input is empty
//the isEmpty public method is called with 2 arguments, one for the form variable and one for the error message
//the method internally calls the _getValue method and then trim()s the result to check if it is empty
// if the variable is empty, a new element is added class variable $_errorList (an array) to represent the error. This new element is itself a three-element associative array, with the keys "field", "value" and "msg", representing the form variable name, the current value of that variable, and the corresponding error message
// if the variable is not empty, it will pass the test and return true
// this function, along with _getValue, allows the user to get both the field name and the field value
//the $this prefix provides a convenient way to access variables and functions which are "local" to the class.
function isEmpty($field, $msg) {
$value = $this->_getValue($field);
if (trim($value) == "") {
$this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);
return false;
}
else {
return true;
}
}
//check whether input is a string
function isString($field, $msg) {
$value = $this->_getValue($field);
if(!is_string($value)) {
$this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);
return false;
}
else {
return true;
}
}
//check whether input is a number
function isNumber($field, $msg) {
$value = $this->_getValue($field);
if(!is_numeric($value)) {
$this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);
return false;
}
else {
return true;
}
}
//check whether input is an integer
function isInteger($field, $msg) {
$value = $this->_getValue($field);
if(!is_integer($value)) {
$this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);
return false;
}
else {
return true;
}
}
//check whether input is a float
function isFloat($field, $msg) {
$value = $this->_getValue($field);
if(!is_float($value)) {
$this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);
return false;
}
else {
return true;
}
}
//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;
}
}
//check whether the input is alphanumeric
function isAlpha($field, $msg) {
$value = $this->_getValue($field);
$pattern = "/^[a-zA-Z]+$/";
if (preg_match($pattern, $value)) {
return true;
}
else {
$this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);
return false;
}
}
//check whether input is a valid email address
function isEmailAddress($field, $msg) {
$value = $this->_getValue($field);
$pattern =
"/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/
";
if (preg_match($pattern, $value)) {
return true;
}
else {
$this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);
return false;
}
}
//end basic validation routines-----
//return the list of current errors
function getErrorList() {
return $this->_errorList;
}
//check whether any errors have occured in validation
//this method checks the size of the $_errorList array; if the size is greater than one, it implies that one or more errors have occured during validation
//it returns a Boolean value
function isError() {
if(sizeof($this->_errorList) > 0) {
return true;
}
else {
return false;
}
}
//reset the error list to a blank array
function resetErrorList() {
$this->_errorList = array();
}
}//end FormValidator class
?>
-------Here is the form (simpleform.html)
<html>
<head>
<basefont face="Arial">
</head>
<body>
<form action="processor.php" method="POST">
<b>Name:</b>
<br>
<input type="text" name="name" size="15">
<p>
<b>Age:</b>
<br>
<input type="text" name="age" size="2" maxlength="2">
<p>
<b>Sex:</b>
<br>
<input type="Radio" name="sex" value="m">Male
<input type="Radio" name="sex" value="f">Female
<p>
<b>Favourite sandwich type:</b>
<br>
<select name="stype">
<option value="">-select one-</option>
<option value="1">Thin crust</option>
<option value="2">Thick crust</option>
<option value="3">Toasted</option>
</select>
<p>
<b>Favourite sandwich filling:</b>
<br>
<input type="Checkbox" name="sfill[]" value="BLT">Bacon, lettuce tomato
<input type="Checkbox" name="sfill[]" value="EC">Egg and cheese
<input type="Checkbox" name="sfill[]" value="PBJ">Peanut butter and jelly
<p>
<input type="Submit" name="submit" value="Save">
</form>
</body>
</html>
------and here is processor.php
<?php
//include class
include("FormValidator.class.inc");
// instantiate object
$fv = new FormValidator();
//perform validation
$fv->isEmpty("name", "Please enter a name");
$fv->isNumber("age", "Please enter a valid age");
$fv->isWithinRange("age", "Please enter an age within the numeric range 1-99", 1, 99);
$fv->isEmpty("sex", "Please enter your sex");
$fv->isEmpty("stype", "Please select one of the listed sandwich types");
$fv->isEmpty("sfill", "Please select one or more of the listed sandwich fillings");
if($fv->isError()) {
$errors = $fv->getErrorList();
echo "<b>The operation could not be performed because one or more error(s) occurred.</b> <p> Please resubmit the form after making the following changes:";
echo "<ul>";
foreach($errors as $e) {
echo "<li>" . $e['msg'] . "</li>";
}
echo "</ul>";
}
else {
//do something useful with the data
echo "Data OK";
}
?>