Zend Framework常用校驗(yàn)器詳解
本文實(shí)例講述了Zend Framework常用校驗(yàn)器。分享給大家供大家參考,具體如下:
Date日期校驗(yàn)器
代碼:
<?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."無(wú)效!<p>"; } } $date1 = "2008-02-15"; $date2 = "2008-02-31"; $date3 = "02-15-2008"; c_date($date1); c_date($date2); c_date($date3);
結(jié)果:
輸入的日期格式:2008-02-15有效!
輸入的日期格式:2008-02-31無(wú)效!
輸入的日期格式:02-15-2008無(wú)效!
點(diǎn)評(píng):源碼解析
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數(shù)組包含校驗(yàn)器
如果內(nèi)容包含在數(shù)組中將返回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 "指定的內(nèi)容:"; echo $n.",存在于指定數(shù)組中!<p>"; }else{ echo "指定的內(nèi)容:"; echo $n.",不存在于指定數(shù)組中!<p>"; } } $city1 = "北京"; $city2 = "重慶"; $city3 = "鄭州"; c_array($city1); c_array($city2); c_array($city3);
結(jié)果:
指定的內(nèi)容:北京,存在于指定數(shù)組中!
指定的內(nèi)容:重慶,存在于指定數(shù)組中!
指定的內(nèi)容:鄭州,不存在于指定數(shù)組中!
Regex正則匹配校驗(yàn)器
通過(guò)使用正則表達(dá)式,再加上合理使用本校驗(yàn)器,幾乎可以實(shí)現(xiàn)所有的校驗(yàn)規(guī)則。
代碼:
<?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\">指定的內(nèi)容:"; echo $v."<p>符合定義的正規(guī)規(guī)則!</font>"; echo "<p>"; }else{ echo "<font color=\"#ff0000\">指定的內(nèi)容:"; echo $v."<p>不符合定義的正規(guī)規(guī)則!</font>"; echo "<p>"; } } $temp1 = "ab"; $temp2 = "abb"; $temp3 = "abbb"; c_rege($temp1); c_rege($temp2); c_rege($temp3);
結(jié)果:
指定的內(nèi)容:ab
不符合定義的正規(guī)規(guī)則!
指定的內(nèi)容:abb
符合定義的正規(guī)規(guī)則!
指定的內(nèi)容:abbb
符合定義的正規(guī)規(guī)則!
點(diǎn)評(píng):
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); }
構(gòu)造函數(shù)初始化私有屬性,
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; }
進(jìn)行驗(yàn)證工作。
自定義校驗(yàn)器編寫
繼承Zend_Validate_Interface接口實(shí)現(xiàn)用戶自定義校驗(yàn)器。
代碼案例,功能判斷指定數(shù)值是否為3的倍數(shù)。
接口代碼:
<?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(); }
要實(shí)現(xiàn)其中的兩個(gè)方法,一個(gè)是isValid(),一個(gè)是getMessages()
實(shí)現(xiàn)代碼:
<?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 "指定的數(shù)值:"; echo $n.",是3的倍數(shù)!<p>"; }else{ echo "指定的數(shù)值:"; echo $n.",不是3的倍數(shù)!<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);
結(jié)果:
指定的數(shù)值:5,不是3的倍數(shù)!
失敗的消息為:
'5'不能被3整除
指定的數(shù)值:6,是3的倍數(shù)!
指定的數(shù)值:8,不是3的倍數(shù)!
失敗的消息為:
'8'不能被3整除
點(diǎn)評(píng):
這里通過(guò)isValid()方法來(lái)設(shè)置屬性信息,通過(guò)getMessages()方法來(lái)獲取錯(cuò)誤消息。錯(cuò)誤消息是一個(gè)數(shù)組,通過(guò)foreach()方法來(lái)遍歷讀取。
更多關(guān)于zend相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《Yii框架入門及常用技巧總結(jié)》、《ThinkPHP入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Zend Framework框架的PHP程序設(shè)計(jì)有所幫助。
- Zend Framework教程之前端控制器Zend_Controller_Front用法詳解
- Zend Framework動(dòng)作控制器用法示例
- Zend Framework路由器用法實(shí)例詳解
- Zend Framework分發(fā)器用法示例
- Zend Framework過(guò)濾器Zend_Filter用法詳解
- Zend Framework實(shí)現(xiàn)自定義過(guò)濾器的方法
- Zend Framework校驗(yàn)器Zend_Validate用法詳解
- Zend Framework教程之分發(fā)器Zend_Controller_Dispatcher用法詳解
- Zend Framework 2.0事件管理器(The EventManager)入門教程
- Zend Framework前端控制器用法示例
相關(guān)文章
如何用phpmyadmin設(shè)置mysql數(shù)據(jù)庫(kù)用戶的權(quán)限
發(fā)現(xiàn)有很多用戶對(duì)數(shù)據(jù)庫(kù)用戶權(quán)限的設(shè)置不太了解,下面為大家詳細(xì)講解一下如何用 phpMyAdmin 來(lái)設(shè)置數(shù)據(jù)庫(kù)用戶的權(quán)限2012-01-01php實(shí)現(xiàn)文章評(píng)論系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)文章評(píng)論系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02Thinkphp頁(yè)面跳轉(zhuǎn)設(shè)置跳轉(zhuǎn)等待時(shí)間的操作
今天小編就為大家分享一篇Thinkphp頁(yè)面跳轉(zhuǎn)設(shè)置跳轉(zhuǎn)等待時(shí)間的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10一個(gè)簡(jiǎn)單安全的PHP驗(yàn)證碼類、PHP驗(yàn)證碼
這篇文章主要介紹了一個(gè)簡(jiǎn)單安全的PHP驗(yàn)證碼類 PHP驗(yàn)證碼的相關(guān)資料,需要的朋友可以參考下2016-09-09windwos下使用php連接oracle數(shù)據(jù)庫(kù)的過(guò)程分享
這篇文章主要介紹了windwos下使用php連接oracle數(shù)據(jù)庫(kù)的過(guò)程分享,講解了php連接oracle的必要條件、代碼實(shí)例以及錯(cuò)誤排查等,需要的朋友可以參考下2014-05-05PHP+jQuery翻板抽獎(jiǎng)功能實(shí)現(xiàn)
在電視節(jié)目中有一種抽獎(jiǎng)形式暫且叫做翻板抽獎(jiǎng),臺(tái)上有一個(gè)墻面,墻面放置幾個(gè)大方塊,主持人或者抽獎(jiǎng)?wù)叻_(kāi)對(duì)應(yīng)的方塊即可揭曉中獎(jiǎng)結(jié)果。類似的抽獎(jiǎng)形式還可以應(yīng)用在WEB中,本文將使用PHP+jQuery為您講解如何實(shí)現(xiàn)翻板抽獎(jiǎng)程序。2015-10-10