May 17, 2007
Handling Form Errors in PHP
Since most of web development revolves around form processing i made this crude Error handling class a while back. Its very simple containing
1. isEmpty
2. isValidLength
3. isNumerical
4. isAlpha …and more. enjoy.
/*************************** * Name: Error.class.php * Description: Handles error checking and reporting * throught out the site. ****************************/ class Error { var $message; /********************** * CONSTTRUCTOR **********************/ function Error(){} /********************** * DESTRUCTOR **********************/ function __destruct(){} function getMessage(){ return $this->message; } /************************ * IsEmpty: * Description: Checks if the parameters is empty * Parameters: (String) Parameter Name | (String) Parameter Values ************************/ function isEmpty($parameterName, $parameterValue){ $parameterValue = trim($parameterValue); if(empty($parameterValue)){ $this->message = "The ".$parameterName." field can not be empty."; return true; }else{ return false; } }//End isEmpty /************************ * isValidLength: * Description: Checks if the value is proper length. * Parameters: (String) Parameter Name | (String) Parameter Values | (Int) Max Length ************************/ function isValidLength($parameterName, $parameterValue, $maxLength){ if(strlen($parameterValue) > $maxLength){ $this->message = "The ".$parameterName." value is longer than the allowed size."; return false; }else{ return true; } }//End isValidLength /************************ * isNumerical: * Description: Checks if the value is only numerical digits * Parameters: (String) Parameter Name | (String) Parameter Values ************************/ function isNumerical($parameterName, $parameterValue){ if(!is_numeric($parameterValue)){ $this->message = "The ".$parameterName." field contains non-numeric illegal characters"; return false; }else{ return true; } }//End isNumerical /************************ * isAlpha: * Description: Checks if the value contains only letters * Parameters: (String) Parameter Name | (String) Parameter Values ************************/ function isAlpha($parameterName, $parameterValue){ if(!preg_match("/[A-Z]*/i", $parameterValue)){ $this->message = "The ".$parameterName." field contains non-alpha illegal characters."; return false; }else{ return true; } } /************************ * isValidString: * Description: Generic Validator - checks that the value does not contain invalid characters * Parameters: (Array) Array of invalid characters | (String) Parameter Value ************************/ function isValidString($parameterName, $parameterValue, $format){ if(!preg_match("/".$format."/i", $parameterValue)){ $this->message = "The ".$parameterName." field contains illegal characters."; return false; }else{ return true; } }//End isValidString /************************ * isFormat: * Description: Check is the string has th4e right format * Parameters: (String) Reg Ex | (String) Parameter Value ************************/ function isFormat($parameterName, $parameterValue, $format){ if(!eregi($format, $parameterValue)){ $this->message = "The ".$parameterName." field contains a wrong formated value."; return false; }else{ return true; } }//End isFormat }//End class ?>
Armando Padilla