国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

Zend Framework常用校驗器詳解

2024-05-04 23:53:27
字體:
來源:轉載
供稿:網友

本文實例講述了Zend Framework常用校驗器。分享給大家供大家參考,具體如下:

Date日期校驗器

代碼:

<?phprequire_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。

代碼:

<?phprequire_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正則匹配校驗器

通過使用正則表達式,再加上合理使用本校驗器,幾乎可以實現所有的校驗規則。

代碼:

<?phprequire_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()

實現代碼:

<?phprequire_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 Framework框架的PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 乐山市| 申扎县| 江永县| 阿合奇县| 辽中县| 肃宁县| 加查县| 安图县| 报价| 青阳县| 大足县| 论坛| 曲周县| 高尔夫| 博爱县| 界首市| 合肥市| 方山县| 正安县| 海阳市| 平定县| 基隆市| 聂荣县| 乌鲁木齐县| 沛县| 内丘县| 忻州市| 弋阳县| 正安县| 商丘市| 衡东县| 房产| 涞水县| 临夏县| 昔阳县| 洪湖市| 大竹县| 新竹市| 苍梧县| 凤冈县| 钟祥市|