PHP Classes

File: formchecker.doc.en.txt

Recommend this page to a friend!
  Classes of Johan Barbier   Form Checker   formchecker.doc.en.txt   Download  
File: formchecker.doc.en.txt
Role: Documentation
Content type: text/plain
Description: English doc
Class: Form Checker
Sanitize and validate user input data
Author: By
Last change: added doc
Date: 17 years ago
Size: 6,648 bytes
 

Contents

Class file image Download
FORMCHECKER PHP5 Data validation package Instanciation : formChecker::__construct ($loc = 'FRA'); $loc (string) => alpha 3 country code localization string Example : $oChecker = new formChecker ('FRA'); Public methods : formChecker::validate ($mString, $sName, $cPattern, $aOptions = null, $bMandatory = true) apply a validation pattern for the data $mString (string) => data to be validated $sName (string) => user defined name to be applied to the data. Used in the returned messages. $cPattern (formChecker const) => validation pattern constant $aOptions (array) (optionnel) => array of options. null if no option $bMandatory (boolean) (optionnel) => true if the data is mandaotry (empty = not valid), false if not (empty = valid). @return (boolean) true is data is valid, false if not. Example $bRes = $oChecker -> validate ('12345', 'zip code', formChecker::VALIDATE_INT, array ('strict_length' => 5, 'not_starting_with' => array ('00'))) formchecker::sanitizeStr ($mString, $aOptions = array ('addslashes', 'htmlentities')) Sanitize the data as a string $mString (string) => data to be sanitized $aOptions (array) => array of options @return (string) sanitized data Available options : addslashes => apply the addslashes () function to the data htmlentities => apply the htmlentities () function to the data urlencode => apply the urlencode () function to the data trim => apply the trim () function to the data mssqlEscape => escape quotes for mssql (' => '') Example : $maChaine = $oChecker -> sanitizeStr ('maChaine ', array ('trim')); formChecker::sanitizeInt ($mString, $aOptions = array ('onlyInt')) Sanitize the data as an integer $mString (string) => data to be sanitized $aOptions (array) => array of options @return (string) sanitized data Available options : onlyInt => apply a regular expression to retrieve ONLY integer characters in the data Example : $myInt = $oChecker -> sanitizeInt ('12-32-15', array ('onlyInt')); // $myInt = '123215' formChecker::sanitizeUserFunc ($mString, $aOptions = array ('user_func' => '', 'user_func_params' => array ())) Sanitize the data using a user defined function $mString (string) => data to be sanitized $aOptions (array) => array of options @return (string) sanitized data Options : user_func => mandaotory. Value MUST be the name of the function to be used. user_func_params => optional. Must be an array of parameters waited by the user defined function LESS the data itself. So the function must accept AT LEAST 1 parameter : the data itself. If there ara more parameters, the data MUST BE in the last parameter. Example : $sToBeSanitized = $oChecker -> sanitizeUserFunc ($sToBeSanitized, array ('user_func' => 'mySanitizer')); Pattern : These patterns are class constants. const VALIDATE_EMAIL => Email validation pattern const VALIDATE_INT => Integer validation pattern const VALIDATE_TEXT => Text validation pattern const VALIDATE_URL => URL validation pattern const VALIDATE_REGEXP = => User defined regular expression validation pattern const VALIDATE_USER_FUNC = => User defined function validation pattern Options : These are options to be applied to the used pattern. You can cumulate these options. For all the patterns : forbidden => (string) ou (array) gives the value(s) forbidden for the data Example : $bRes = $oChecker -> validate ('12345', 'zip code', formChecker::VALIDATE_INT, array ('forbidden' => array ('12345', '6789'))) or $bRes = $oChecker -> validate ('12345', 'zip code', formChecker::VALIDATE_INT, array ('forbidden' => '12345')) allowed => (string) ou (array) gives the allowed value(s) for the data Example : $bRes = $oChecker -> validate ('12345', 'zip code', formChecker::VALIDATE_INT, array ('allowed' => array ('12345', '6789'))) or $bRes = $oChecker -> validate ('12345', 'zip code', formChecker::VALIDATE_INT, array ('allowed' => '12345')) min_length => (int) minimal length for the data Example : $bRes = $oChecker -> validate ('12345', 'zip code', formChecker::VALIDATE_INT, array ('min_length' => 6)) max_length => (int) maximal length for the data Example : $bRes = $oChecker -> validate ('12345', 'zip code', formChecker::VALIDATE_INT, array ('max_length' => 4)) strict_length => (int) mandatory length for the data Example : $bRes = $oChecker -> validate ('12345', 'zip code', formChecker::VALIDATE_INT, array ('strict_length' => 5)) starting_with => (array) array of values the data must start with (at least 1) Example : $bRes = $oChecker -> validate ('12345', 'zip code', formChecker::VALIDATE_INT, array ('starting_with' => array ('1', '3', '5'))) not_starting_with => (array) array of values the data must NOT start with Example : $bRes = $oChecker -> validate ('12345', 'zip code', formChecker::VALIDATE_INT, array ('not_starting_with' => array ('1', '3', '5'))) Regexp pattern only (formChecker::VALIDATE_REGEXP) regexp => (string) user defined regular expression that must be used to validate the data Example : $bRes = $oChecker -> validate ('12345', 'zip code', formChecker::VALIDATE_REGEXP, array ('regexp' => '@(bla|bli)@')) User defined function pattern only (formChecker::VALIDATE_USER_FUNC) user_func => (string) user defined function name used to validate the data Example : $bRes = $oChecker -> validate ('12345', 'zip code', formChecker::VALIDATE_USER_FUNC, array ('user_func' => 'myFunc')) user_func_params => (array) array of parameters needed by the function LESS the data itself Example : $bRes = $oChecker -> validate ('12345', 'zip code', formChecker::VALIDATE_USER_FUNC, array ('user_func' => 'myFunc', 'user_func_params' => array ('param'))) NOTICE : this pattern's validation method will anyway add at the end of the array of parameters the data (even if you have no parameter). So, your function MUST implements at least 1 parameter (the data itself), and if there are more paramaters, the data MUST be in the last parameter of your function. Your function MUST return a boolean : true or false; true if validation is ok, false if not. The pattern does not wait for any other return value. Integer pattern only (formChecker::VALIDATE_INT) min_range => (int) minimum value for the data Example : $bRes = $oChecker -> validate ('12345', 'zip code', formChecker::VALIDATE_INT, array ('min_range' => 12344)) max_range => (int) maximum value for the data Example : $bRes = $oChecker -> validate ('12345', 'zip code', formChecker::VALIDATE_INT, array ('max_range' => 12346))