The examples in this article describe Zend Framework common validators. Share it with everyone for your reference, the details are as follows:
Date date validator
Code:
<?php require_once 'Zend/Validate/Date.php'; function c_date($date){ $validator = new Zend_Validate_Date(); if($validator->isValid($date)){ echo "输入的日期格式:"; echo $date."有效!<p>"; }else{ echo "输入的日期格式:"; echo $date."无效!<p>"; } } $date1 = "2008-02-15"; $date2 = "2008-02-31"; $date3 = "02-15-2008"; c_date($date1); c_date($date2); c_date($date3);
Result:
Input date format: 2008 -02-15 is valid!
The entered date format: 2008-02-31 is invalid!
The entered date format: 02-15-2008 is invalid!
Comments: Source code analysis
public function isValid($value) { if (!is_string($value) && !is_int($value) && !is_float($value) && !is_array($value) && !($value instanceof Zend_Date)) { $this->_error(self::INVALID); return false; } $this->_setValue($value); if (($this->_format !== null) || ($this->_locale !== null) || is_array($value) || $value instanceof Zend_Date) { require_once 'Zend/Date.php'; if (!Zend_Date::isDate($value, $this->_format, $this->_locale)) { if ($this->_checkFormat($value) === false) { $this->_error(self::FALSEFORMAT); } else { $this->_error(self::INVALID_DATE); } return false; } } else { if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) { $this->_format = 'yyyy-MM-dd'; $this->_error(self::FALSEFORMAT); $this->_format = null; return false; } list($year, $month, $day) = sscanf($value, '%d-%d-%d'); if (!checkdate($month, $day, $year)) { $this->_error(self::INVALID_DATE); return false; } } return true; }
InArray array contains validator
If the content is included in the array, True will be returned, otherwise False will be returned.
Code:
<?php require_once 'Zend/Validate/InArray.php'; function c_array($n){ $temp = array("北京","上海","天津","重庆"); $validator = new Zend_Validate_InArray($temp); if($validator->isValid($n)){ echo "指定的内容:"; echo $n.",存在于指定数组中!<p>"; }else{ echo "指定的内容:"; echo $n.",不存在于指定数组中!<p>"; } } $city1 = "北京"; $city2 = "重庆"; $city3 = "郑州"; c_array($city1); c_array($city2); c_array($city3);
Result:
Specified content: Beijing, exists in the specified array!
Specified content: Chongqing, exists in the specified array!
Specified content: Zhengzhou, does not exist in the specified array!
Regex regular matching validator
By using regular expressions and using this validator rationally, almost all verification rules can be implemented.
Code:
<?php require_once "Zend/Validate.php"; function c_rege($v){ $pattern = array("/ab{2,}/"); if(Zend_Validate::is($v,"Regex",$pattern)){ echo "<font color=\"#006600\">指定的内容:"; echo $v."<p>符合定义的正规规则!</font>"; echo "<p>"; }else{ echo "<font color=\"#ff0000\">指定的内容:"; echo $v."<p>不符合定义的正规规则!</font>"; echo "<p>"; } } $temp1 = "ab"; $temp2 = "abb"; $temp3 = "abbb"; c_rege($temp1); c_rege($temp2); c_rege($temp3);
Result:
Specified content: ab
does not comply with the defined formal rules!
Specified content: abb
Conforms to the defined formal rules!
Specified content: abbb
Conforms to the defined formal rules!
Comments:
public function __construct($pattern) { if ($pattern instanceof Zend_Config) { $pattern = $pattern->toArray(); } if (is_array($pattern)) { if (array_key_exists('pattern', $pattern)) { $pattern = $pattern['pattern']; } else { require_once 'Zend/Validate/Exception.php'; throw new Zend_Validate_Exception("Missing option 'pattern'"); } } $this->setPattern($pattern); }
The constructor initializes the private properties,
public function isValid($value) { if (!is_string($value) && !is_int($value) && !is_float($value)) { $this->_error(self::INVALID); return false; } $this->_setValue($value); $status = @preg_match($this->_pattern, $value); if (false === $status) { $this->_error(self::ERROROUS); return false; } if (!$status) { $this->_error(self::NOT_MATCH); return false; } return true; }
performs verification work.
Writing custom validator
Inherit the Zend_Validate_Interface interface to implement user-defined validator.
Code example, the function determines whether the specified value is a multiple of 3.
Interface code:
<?php /** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@zend.com so we can send you a copy immediately. * * @category Zend * @package Zend_Validate * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @version $Id: Interface.php 24593 2012-01-05 20:35:02Z matthew $ */ /** * @category Zend * @package Zend_Validate * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ interface Zend_Validate_Interface { /** * Returns true if and only if $value meets the validation requirements * * If $value fails validation, then this method returns false, and * getMessages() will return an array of messages that explain why the * validation failed. * * @param mixed $value * @return boolean * @throws Zend_Validate_Exception If validation of $value is impossible */ public function isValid($value); /** * Returns an array of messages that explain why the most recent isValid() * call returned false. The array keys are validation failure message identifiers, * and the array values are the corresponding human-readable message strings. * * If isValid() was never called or if the most recent isValid() call * returned true, then this method returns an empty array. * * @return array */ public function getMessages(); }
To implement two of the methods, one is isValid() and the other is getMessages()
Implementation code:
<?php require_once "Zend/Validate/Interface.php"; class MyValidator implements Zend_Validate_Interface{ protected $_messages = array(); public function isValid($value){ $this->_messages = array(); $requirement = !($value%3); if(!$requirement){ $this->_messages[] = "'$value'不能被3整除"; return false; } return true; } public function getMessages(){ return $this->_messages; } } function c_n_3($n){ $validator = new MyValidator(); if($validator->isValid($n)){ echo "指定的数值:"; echo $n.",是3的倍数!<p>"; }else{ echo "指定的数值:"; echo $n.",不是3的倍数!<p>"; echo "失败的消息为:<p>"; foreach ($validator->getMessages() as $message) { echo "$message<p>"; } } } $num1 = 5; $num2 = 6; $num3 = 8; c_n_3($num1); c_n_3($num2); c_n_3($num3);
Result:
Specified value: 5, not a multiple of 3!
The failure message is:
'5' is not divisible by 3
The specified value: 6, which is a multiple of 3!
Specified value: 8, not a multiple of 3!
The failure message is:
'8' is not divisible by 3
Comments:
Here, the attribute information is set through the isValid() method. getMessages() method to get error messages. The error message is an array, which is traversed and read through the foreach() method.
I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.
For more detailed explanations of commonly used validators in Zend Framework, please pay attention to the PHP Chinese website!