Zend Framework学习之验证ing
通过setMessage自定义错误信息
代码:
<?php require_once 'Zend/Validate/EmailAddress.php'; function c_email($email) { $validator = new Zend_Validate_EmailAddress(); $validator->setMessage( "%value%不符合标准的E-mail地址格式" ); if($validator->isValid($email)){ echo "输入的E-mail地址:"; echo $email."有效!<p>"; }else{ echo "输入的E-mail地址:"; echo $email."无效!"; echo "失败消息为:<p>"; $message = $validator->getMessages(); echo $message['emailAddressInvalidFormat']; } } $e_m1 = "abc@123.com"; $e_m2 = "abc#123.com"; c_email($e_m1); c_email($e_m2);
结果:
输入的E-mail地址:abc@123.com有效!
输入的E-mail地址:abc#123.com无效!失败消息为:
abc#123.com不符合标准的E-mail地址格式
点评:
功能,通过setMessage方法设置自己想要的提示信息。
静态方法is()
作用:直接通过is()方法创建校验器,不用每次都去new来创建。
语法:
Zend_Validate::is($value,$classBaseName,$args);
代码:
<?php require_once 'Zend/Validate.php'; function c_num($num){ if(Zend_Validate::is($num,'Between',array(10,40)))//调用is()静态方法,并对结果进行判断 { echo "<font color=\"#006600\">输入的数值:"; echo $num."<p>在10与40之间!</font>"; echo "<p>"; }else{ echo "<font color=\"#ff0000\">输入的数值:"; echo $num."<p>不在10与40之间!</font>"; echo "<p>"; } } $num1 = 5; $num2 = 22; c_num($num1); c_num($num2);
结果:
输入的数值:5
不在10与40之间!
输入的数值:22
在10与40之间!
点评:来看看源码是什么样的?
public static function is($value, $classBaseName, array $args = array(), $namespaces = array()) { $namespaces = array_merge((array) $namespaces, self::$_defaultNamespaces, array('Zend_Validate')); $className = ucfirst($classBaseName); try { if (!class_exists($className, false)) { require_once 'Zend/Loader.php'; foreach($namespaces as $namespace) { $class = $namespace . '_' . $className; $file = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php'; if (Zend_Loader::isReadable($file)) { Zend_Loader::loadClass($class); $className = $class; break; } } } $class = new ReflectionClass($className); if ($class->implementsInterface('Zend_Validate_Interface')) { if ($class->hasMethod('__construct')) { $keys = array_keys($args); $numeric = false; foreach($keys as $key) { if (is_numeric($key)) { $numeric = true; break; } } if ($numeric) { $object = $class->newInstanceArgs($args); } else { $object = $class->newInstance($args); } } else { $object = $class->newInstance(); } return $object->isValid($value); } } catch (Zend_Validate_Exception $ze) { // if there is an exception while validating throw it throw $ze; } catch (Exception $e) { // fallthrough and continue for missing validation classes } require_once 'Zend/Validate/Exception.php'; throw new Zend_Validate_Exception("Validate class not found from basename '$classBaseName'"); }
源码做的事情大概就是把类引入,把条件引入,然后作出判断,返回结果。很神奇。略复杂。
尼玛,真不能小看了PHP。能如此娴熟的运用,太佩服了。
使用校验器链addValidator()
校验器链是多个校验器的一个连接。当所有的校验规则都通过时,将返回True值。
代码:
<?php require_once 'Zend/Validate.php'; require_once 'Zend/Validate/StringLength.php'; require_once 'Zend/Validate/Alnum.php'; function c_content($value){ $v_Chain = new Zend_Validate(); $v_Chain->addValidator(new Zend_Validate_StringLength(8,12)) ->addValidator(new Zend_Validate_Alnum()); if($v_Chain->isValid($value)){ echo "输入的值:"; echo $value."有效!"; }else{ echo "输入的值:"; echo $value."无效!"; echo "失败消息为:<p>"; foreach($v_Chain->getMessages() as $message){ echo "$message<p>"; } } } $temp1 = "123456"; $temp2 = "!@#$%^&*()"; $temp3 = "abcdefgh"; c_content($temp1); c_content($temp2); c_content($temp3);
结果:
输入的值:123456无效!失败消息为:
'123456' is less than 8 characters long
输入的值:!@#$%^&*()无效!失败消息为:
'!@#$%^&*()' contains characters which are non alphabetic and no digits
输入的值:abcdefgh有效!
点评:
让我们来看看内部的奥秘,
public function addValidator(Zend_Validate_Interface $validator, $breakChainOnFailure = false) { $this->_validators[] = array( 'instance' => $validator, 'breakChainOnFailure' => (boolean) $breakChainOnFailure ); return $this; }
addValidator()方法将对象存入私有变量_validators中,
public function isValid($value) { $this->_messages = array(); $this->_errors = array(); $result = true; foreach ($this->_validators as $element) { $validator = $element['instance']; if ($validator->isValid($value)) { continue; } $result = false; $messages = $validator->getMessages(); $this->_messages = array_merge($this->_messages, $messages); $this->_errors = array_merge($this->_errors, array_keys($messages)); if ($element['breakChainOnFailure']) { break; } } return $result; }
验证的时候,进行遍历验证即可。