Sådan benyttes komponenten ValidatorFunction klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/ValidatorFunction.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? ValidatorFunction::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new ValidatorFunction($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten ValidatorFunction klassen
Den fulde PHP kildekode for ValidatorFunction klassen
<?php/** * @package validator * @filesource * @see HTML_VALIDATOR_COMPONENT_PATH.'/ValidatorFunction.php' * @copyright (c) http://Finn-Rasmussen.com * @license http://Finn-Rasmussen.com/license/ myPHP License conditions * @author http://Finn-Rasmussen.com * @version 1.11 * @since 27-nov-2009 *//** * The required files */require_once(HTML_BASIC_UTIL_PATH.'/Singleton.php');/** * Validates the different controls for a form. Ready to use * The first parameter is the control to validate. This control must have e ->get('value') methode * If an error is detected the second parameter and additional information is added to the ValidatorErrorList * <code> * Usage: * // PHP build-in functions * $result = ValidatorFunction::isArray ($control, $msg); * $result = ValidatorFunction::isBoolean ($control, $msg); * $result = ValidatorFunction::isFloat ($control, $msg); * $result = ValidatorFunction::isInt ($control, $msg); * $result = ValidatorFunction::isNumeric ($control, $msg); * $result = ValidatorFunction::isObject ($control, $msg); * $result = ValidatorFunction::isString ($control, $msg); * * Standard validators * $result = ValidatorFunction::inRange ($control, $msg, $min, $max); * $result = ValidatorFunction::isChecked ($control, $msg); * $result = ValidatorFunction::isDate ($control, $msg); * $result = ValidatorFunction::isDomain ($control, $msg); * $result = ValidatorFunction::isEmail ($control, $msg); * $result = ValidatorFunction::isEmpty ($control, $msg); * $result = ValidatorFunction::isRequired($control, $msg); * * Logical expression supplied as $is * $result = ValidatorFunction::is ($is, $control, $msg); * $result = ValidatorFunction::isValid ($is, $control, $msg); * * Customer specific * $result = ValidatorFunction::isCompany($control, $msg); * $result = ValidatorFunction::isAddress($control, $msg); * $result = ValidatorFunction::isZip($control, $msg); * $result = ValidatorFunction::isCity($control, $msg); * $result = ValidatorFunction::isPhoneNumber($control, $msg); // mobile, private and business * $result = ValidatorFunction::isEAN($control, $msg); * // email, contact person are a standard validators * * Login specific * $result = ValidatorFunction::isUsername($control, $msg, $min); * $result = ValidatorFunction::isPassword($control, $msg, $min); * // email is a standard validator * * Product * $result = ValidatorFunction::isDomain($control, $msg); * $result = ValidatorFunction::isHeader($control, $msg); * $result = ValidatorFunction::isDescription($control, $msg); * // price, checked are standard validators * * Basket * // Text, quantity and price are standard validators * * TODO * $result = ValidatorFunction::isCPR($control, $msg); * $result = ValidatorFunction::isUrl($control, $msg); * $result = ValidatorFunction::isPhoneNumber($control, $msg); * </code> * @package validator */class ValidatorFunction { /** * Constructor * @private */ function __construct() { } // *** start PHP build in functions /** * Returns true if the value of the control is is_function else return false * <code> * $rc = ValidatorFunction::is('is_string', $control, $msg); * </code> * @see http://php.net/manual/da/function.is-array.php * @static * @param String $is_function The 'is_function' to use as validator * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the value of the control is is_function */ public static function is($is_function, $control, $msg) { $isValid = true; $attribute = $control->get(HTML_ATTRIBUTE_VALUE); if (is_array($attribute)) { // TODO add validator functionality for multiple lines, like in a basket// print("ValidatorFunction::is() $attribute<br />".$control->getHtml());// foreach($attribute as $key=>$value) {// print "$key=>$value<br />";// }// die("ValidatorFunction::is() Arrray detected, $msg"); $isValid = true; // SKIP validation, when multiple lines } else { // OK $isValid = ValidatorFunction::isValid($is_function($attribute), $control, $msg);; } return $isValid; } /** * Returns true if the value of the control is a string else return false * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the value of the control is a string */ public static function isString($control, $msg) { return ValidatorFunction::is('is_string', $control, $msg); } /** * Returns true if the value of the control is numeric else return false * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the value of the control is numeric */ public static function isNumeric($control, $msg) { return ValidatorFunction::is('is_numeric', $control, $msg); } /** * Returns true if the value of the control is boolean else return false * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the value of the control is boolean */ public static function isBoolean($control, $msg) { return ValidatorFunction::is('is_bool', $control, $msg); } /** * Returns true if the value of the control is float else return false * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the value of the control is float */ public static function isFloat($control, $msg) { return ValidatorFunction::is('is_float', $control, $msg); } /** * Returns true if the value of the control is int else return false * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the value of the control is int */ public static function isInt($control, $msg) { return ValidatorFunction::is('is_int', $control, $msg); } /** * Returns true if the value of the control is array else return false * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the value of the control is array */ public static function isArray($control, $msg) { return ValidatorFunction::is('is_array', $control, $msg); } /** * Returns true if the value of the control is object else return false * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the value of the control is object */ public static function isObject($control, $msg) { return ValidatorFunction::is('is_object', $control, $msg); } // *** end PHP build in functions /** * Returns true if the value of the control is in the range between the min/max else return false * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @param int $min The minimum number of characters * @param int $max The maximum number of characters * @return boolean Return true if the expression is in the range between the min/max */ public static function inRange($control, $msg, $min, $max) { $rc = true; $rc &= ValidatorFunction::isValid(strlen($control->get(HTML_ATTRIBUTE_VALUE)) >= $min, $control, $msg); $rc &= ValidatorFunction::isValid(strlen($control->get(HTML_ATTRIBUTE_VALUE)) <= $max, $control, $msg); return $rc; } /** * Returns true if the value of the control is empty else return false * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the expression is empty */ public static function isEmpty($control, $msg) { $isValid = true; $attribute = $control->get(HTML_ATTRIBUTE_VALUE); if (is_array($attribute)) { // SHOULD never end up here// print("ValidatorFunction::is() $attribute<br />".$control->getHtml());// foreach($attribute as $key=>$value) {// print "$key=>$value<br />";// }// die("ValidatorFunction::isEmpty() Arrray detected, $msg"); $isValid = false; // SKIP validation, when multiple lines } else { // OK $value = trim($attribute); $isValid = !ValidatorFunction::isValid(isset($value) && $value !== '', $control, $msg); } return $isValid; } /** * Returns true if the value of the control is checked and not empty else return false * This methode is used primarily to test checkboxes if checked or not * * NOTE: * The checkbox could have the value attribute defined to some value or just send the 'on' value * * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the expression is required and not empty */ public static function isChecked($control, $msg) { $value = trim($control->get(HTML_ATTRIBUTE_VALUE)); if ($value == '') { // Look for a get request with a NOT empty value attribute $value = isset($_GET[$control->get(HTML_ATTRIBUTE_NAME)]) && $_GET[$control->get(HTML_ATTRIBUTE_NAME)]; } if ($value == '') { // Look for a post request with a NOT empty value attribute $value = isset($_POST[$control->get(HTML_ATTRIBUTE_NAME)]) && $_POST[$control->get(HTML_ATTRIBUTE_NAME)]; } return ValidatorFunction::isValid($value != '', $control, $msg); } /** * Returns true if the value of the control is required and not empty else return false * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the expression is required and not empty */ public static function isRequired($control, $msg) { return !ValidatorFunction::isEmpty($control, $msg); } /** * Returns true if the value of the control is valid else return false * This is where the error message handling is taken place * <code> * $control = new Password('theName'); * $msg = 'The error message'; * $is = $control->get(HTML_ATTRIBUTE_NAME) != ''; * $rc = ValidatorFunction::isValid($is, $control, $msg); * </code> * @static * @param boolean $is The validation status of the control true or false * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the value of the control is is_function */ public static function isValid($is, $control, $msg) { if ($is) { // Ok is valid (true) do nothing } else { $errorlist = & Singleton::getInstance(CLASS_VALIDATOR_ERROR_LIST); $errorlist->add(array( HTML_ATTRIBUTE_NAME=>$control->get(HTML_ATTRIBUTE_NAME), HTML_ATTRIBUTE_VALUE=>$control->get(HTML_ATTRIBUTE_VALUE), HTML_ATTRIBUTE_MSG=>$msg, HTML_ATTRIBUTE_ID=>$control->get(HTML_ATTRIBUTE_ID), ), $control->get(HTML_ATTRIBUTE_NAME)); } return $is; } /** * Returns true if the value of the control is exactly 13 numbers or 0 else return false * Sample: 9876543210123 * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the expression is in the range between the min/max */ public static function isEAN($control, $msg) { $rc = true; $ean = 13; $value = trim($control->get(HTML_ATTRIBUTE_VALUE)); $length = strlen($value); if ($length > 0) { $rc &= ValidatorFunction::isNumeric($control, $msg." (ikke et tal=$value)");// TODO sprintf() $rc &= ValidatorFunction::isValid($length == $ean, $control, $msg." (".VALIDATOR_TEXT_FOUND."=$length, ".VALIDATOR_TEXT_EXPECTED."=$ean)"); } return $rc; } /** * Returns true if the value of the control is exactly 4 numbers else return false * Sample: 3400 (a city called Hillerød, in denmark) * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the expression is exactly 4 digit */ public static function isZip($control, $msg) { $rc = true; $zip = 4; $value = trim($control->get(HTML_ATTRIBUTE_VALUE)); $length = strlen($value); $rc &= ValidatorFunction::isNumeric($control, $msg); // TODO sprintf() $rc &= ValidatorFunction::isValid($length == $zip, $control, $msg." (".VALIDATOR_TEXT_FOUND."=$length, ".VALIDATOR_TEXT_EXPECTED."=$zip)"); /** * TODO Danish speciality, only legal zip numbers are allowed for Denmark */ if ($rc && defined('HTML_POSTNO_COMPONENT_PATH')) { require_once(HTML_POSTNO_COMPONENT_PATH.'/Postno.php'); $city = Postno::get($value); $rc &= ValidatorFunction::isValid($city !== '', $control, $msg." (Ukendt by, $city)"); } return $rc; } /** * Returns true if the value of the control is a valid city name else return false * Sample: Kongens Vænge 79 (where I live) * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the expression is a string */ public static function isCity($control, $msg, $min=4) { $rc = true; $value = trim($control->get(HTML_ATTRIBUTE_VALUE)); $length = strlen($value); if ($length > 0) { $rc &= ValidatorFunction::isValid($length >= $min, $control, $msg); } /** * TODO Danish speciality, only legal zip numbers are allowed for Denmark */ if ($rc && defined('HTML_POSTNO_COMPONENT_PATH')) {// TODO, test for valid city names in Denmark // require_once(HTML_POSTNO_COMPONENT_PATH.'/Postno.php');// $city = Postno::get($value);// $rc &= ValidatorFunction::isValid($city !== '', $control, $msg." (Ukendt by, $city)"); } return $rc; } /** * Returns true if the value of the control is a valid email adress else return false * Sample: myself@finn-rasmussen.com * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the expression is in the range between the min/max */ public static function isEmail($control, $msg) { $rc = true; $value = trim($control->get(HTML_ATTRIBUTE_VALUE)); $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'; $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'; $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'; $quoted_pair = '\\x5c\\x00-\\x7f'; $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d"; $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22"; $domain_ref = $atom; $sub_domain = "($domain_ref|$domain_literal)"; $word = "($atom|$quoted_string)"; $domain = "$sub_domain(\\x2e$sub_domain)*"; $local_part = "$word(\\x2e$word)*"; $addr_spec = "$local_part\\x40$domain"; $pm = preg_match("!^$addr_spec$!", $value) ? true : false; // TODO sprintf $rc &= ValidatorFunction::isValid($pm, $control, $msg); // ." (".VALIDATOR_TEXT_FOUND."=$value)"); return $rc; } /** * Returns true if the value of the control is a valid username else return false * Sample: Finn * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @param String $min The minimum length of the field * @return boolean Return true if the expression is exactly 4 digit */ public static function isUsername($control, $msg, $min=4) { $rc = true; $value = trim($control->get(HTML_ATTRIBUTE_VALUE)); $length = strlen($value); if ($length > 0) { $rc &= ValidatorFunction::isValid($length >= $min, $control, $msg." (".VALIDATOR_TEXT_FOUND."=$length, ".VALIDATOR_TEXT_EXPECTED." $min ".VALIDATOR_TEXT_MORE_CHARS.")"); } return $rc; } public static function isHeader($control, $msg, $min=9) { return ValidatorFunction::isUsername($control, $msg, $min); } public static function isDescription($control, $msg, $min=10) { return ValidatorFunction::isUsername($control, $msg, $min); } /** * Returns true if the value of the control is a valid address else return false * Sample: Kongens Vænge 79 (where I live) * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the expression is a string */ public static function isAddress($control, $msg, $min=4) { $rc = true; $value = trim($control->get(HTML_ATTRIBUTE_VALUE)); $length = strlen($value); if ($length > 0) { $rc &= ValidatorFunction::isValid($length >= $min, $control, $msg); } return $rc; } /** * Returns true if the value of the control is a valid company name else return false * Sample: Finn * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the expression is a string */ public static function isCompany($control, $msg, $min=4) { $rc = true; $value = trim($control->get(HTML_ATTRIBUTE_VALUE)); $length = strlen($value); if ($length > 0) { $rc &= ValidatorFunction::isValid($length >= $min, $control, $msg); } return $rc; } /** * Returns true if the value of the control is a valid password else return false * Sample: test0009 * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @param String $min The minimum length of the field * @return boolean Return true if the expression is a valid password */ public static function isPassword($control, $msg, $min=6) { $rc = true; $value = trim($control->get(HTML_ATTRIBUTE_VALUE)); $length = strlen($value); if ($length > 0) { $rc &= ValidatorFunction::isValid($length >= $min, $control, $msg." (".VALIDATOR_TEXT_FOUND."=$length, ".VALIDATOR_TEXT_EXPECTED." $min ".VALIDATOR_TEXT_MORE_CHARS.")"); } return $rc; } /** * Returns true if the value of the control is a valid phone number else return false * Sample: 12345678, this is illegal 12 34 56 78 also this (+45)12345678 * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @param String $min The minimum length of the field * @return boolean Return true if the expression is exactly 8 digit */ public static function isPhoneNumber($control, $msg, $min=8) { $rc = true; $value = trim($control->get(HTML_ATTRIBUTE_VALUE)); $pos = strpos($value, ')'); if ($pos !== false) { $value = substr($value, $pos + 1); } $value = str_replace(' ', '', $value); $length = strlen($value); if ($length > 0) { $rc &= ValidatorFunction::isValid($length == $min, $control, $msg." (".VALIDATOR_TEXT_FOUND."=$length, ".VALIDATOR_TEXT_EXPECTED." $min )"); $rc &= ValidatorFunction::isValid(is_numeric($value), $control, $msg); // TODO sprintf() } return $rc; } /** * * TODO decide if this is realy the validator functionality I want !!!!! * * Returns true if the value of the control is a valid domain else return false * Sample: localhost, finnrasmussen.dk * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @param String $min The minimum length of the field * @return boolean Return true if the expression is exactly 4 digit */ public static function isDomain($control, $msg) { $value = trim($control->get(HTML_ATTRIBUTE_VALUE)); $matches = array("","localhost","finnrasmussen.dk","casanostra.dk","hvepseeksperten.dk",); // TODO add valid domains here $strict = true; $isDomain = in_array($value, $matches, $strict); return ValidatorFunction::isValid($isDomain, $control, $msg); } // ======================================== TODO ========= /** * @todo NOT implemented */ public static function isCPR($control, $msg) { print("ValidatorFunction.php isCPR NOT IMPLEMENTED<br />\r\n"); return true; } /** * Returns true if the value of the control is a valid SQL date/time else return false * Sample: YYYYMMDDHHMMSS, this is illegal 12-4-2010 14:37 * @static * @param Object $control The control to validate * @param String $msg The error message, if validation fails * @return boolean Return true if the expression is exactly a long date */ public static function isSqlDate($control, $msg) { $isDate = false; $isTime = false; $value = trim($control->get(HTML_ATTRIBUTE_VALUE)); $subject = array(); $lenDate = strlen("YYYYMMDD"); if (strlen($value) >= $lenDate) { $subject[0] = substr($value, 0, $lenDate); } if (strlen($value) > $lenDate) { $subject[1] = substr($value, $lenDate); } // Day format check YYYY-MM-DD if (is_array($subject) && count($subject) >= 1) { $parts = array(); $datePattern = "/^([0-9]{4})([0-9]{2})([0-9]{2})/"; // YYYYMMDD $isPregMatchDate = preg_match($datePattern, $subject[0], $parts); if ($isPregMatchDate === 1 && count($parts) > 3) { $checkdate = checkdate($parts[2], $parts[3], $parts[1]); // month, day, year if ($checkdate) { $isDate = true; } else { $isDate = false; $msg .= " (fejl i datoen)"; } } else { $isDate = false; $msg .= "<br />(ukendt dato format)".$subject[0]."<br />count=".count($parts)."<br />"; } } else { $isDate = false; $msg .= " (kan ikke genkende datoen)"; } // Time format check HH:MM:SS if (is_array($subject) && count($subject) === 2) { $parts = array(); $timePattern = "/^([0-9]{2})([0-9]{2})([0-9]{2})/"; // HHMMSS $isPregMatchTime = preg_match($timePattern, $subject[1], $parts); if ($isPregMatchTime === 1 && count($parts) > 3) { // HH: 00 - 23, MM: 00 - 59, SS: 00 - 59 if ($parts[1] >= 00 && $parts[1] <= 23) { if ($parts[2] >= 00 && $parts[2] <= 59) { if ($parts[3] >= 00 && $parts[3] <= 59) { $isTime = true; } else { $isTime = false; $msg .= " (fejl i sekunder)"; } } else { $isTime = false; $msg .= " (fejl i minutter)"; } } else { $isTime = false; $msg .= " (fejl i timer)"; } } else { $isTime = false; $msg .= "<br />(ukendt tidsformat)".$subject[1]."<br />count=".count($parts)."<br />"; } } else { $isTime = true; // Ignore time check $msg .= " (kan ikke genkende tidsformat)"; } return ValidatorFunction::isValid($isDate & $isTime, $control, $msg." value=$value"); } /** * @todo NOT implemented */ public static function isUrl($control, $msg) { $value = trim($control->get(HTML_ATTRIBUTE_VALUE)); $matches = array(); // Get the host name from the URL $isUrl = preg_match("/^(http:\/\/)?([^\/]+)/i", $value, $matches); return ValidatorFunction::isValid($isUrl, $control, $msg); } }?>
Den fulde HTML kildekode for ValidatorFunction klassen
<? Der er ikke fundet noget ?>
Her er 'klasse metoderne' for ValidatorFunction klassen:
Her er 'objekt variable' for ValidatorFunction klassen: