Zend Framework常用校验器详解
本文实例讲述了zend framework常用校验器。分享给大家供大家参考,具体如下:
date日期校验器
代码:
<?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);
结果:
输入的日期格式:2008-02-15有效!
输入的日期格式:2008-02-31无效!
输入的日期格式:02-15-2008无效!
点评:源码解析
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数组包含校验器
如果内容包含在数组中将返回true,否则返回false。
代码:
<?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);
结果:
指定的内容:北京,存在于指定数组中!
指定的内容:重庆,存在于指定数组中!
指定的内容:郑州,不存在于指定数组中!
regex正则匹配校验器
通过使用正则表达式,再加上合理使用本校验器,几乎可以实现所有的校验规则。
代码:
<?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);
结果:
指定的内容:ab
不符合定义的正规规则!
指定的内容:abb
符合定义的正规规则!
指定的内容:abbb
符合定义的正规规则!
点评:
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); }
构造函数初始化私有属性,
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; }
进行验证工作。
自定义校验器编写
继承zend_validate_interface接口实现用户自定义校验器。
代码案例,功能判断指定数值是否为3的倍数。
接口代码:
<?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(); }
要实现其中的两个方法,一个是isvalid(),一个是getmessages()
实现代码:
<?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);
结果:
指定的数值:5,不是3的倍数!
失败的消息为:
'5'不能被3整除
指定的数值:6,是3的倍数!
指定的数值:8,不是3的倍数!
失败的消息为:
'8'不能被3整除
点评:
这里通过isvalid()方法来设置属性信息,通过getmessages()方法来获取错误消息。错误消息是一个数组,通过foreach()方法来遍历读取。
更多关于zend相关内容感兴趣的读者可查看本站专题:《zend framework框架入门教程》、《php优秀开发框架总结》、《yii框架入门及常用技巧总结》、《thinkphp入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于zend framework框架的php程序设计有所帮助。
推荐阅读
-
Zend Framework常用校验器详解
-
Zend Framework使用Zend_Loader组件动态加载文件和类用法详解
-
Zend Framework入门教程之Zend_Db数据库操作详解
-
Spring Boot应用配置常用相关视图解析器详解
-
Spring Boot应用配置常用相关视图解析器详解
-
Zend Framework教程之Zend_Helpers动作助手ViewRenderer用法详解
-
Zend Framework教程之Application和Bootstrap用法详解,zendbootstrap_PHP教程
-
Zend Framework动作助手Url用法详解,zendframework_PHP教程
-
Zend Framework动作助手Redirector用法实例详解,zendredirector_PHP教程
-
Zend Framework教程之前端控制器Zend_Controller_Front用法详解,ledcontroller控制器