亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

首頁 php教程 PHP開發(fā) Zend Framework常用校驗器詳解

Zend Framework常用校驗器詳解

Jan 06, 2017 am 09:59 AM

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

Date日期校驗器

代碼:

<?php
require_once &#39;Zend/Validate/Date.php&#39;;
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);

結(jié)果:

輸入的日期格式: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 &#39;Zend/Date.php&#39;;
      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(&#39;/^\d{4}-\d{2}-\d{2}$/&#39;, $value)) {
        $this->_format = &#39;yyyy-MM-dd&#39;;
        $this->_error(self::FALSEFORMAT);
        $this->_format = null;
        return false;
      }
      list($year, $month, $day) = sscanf($value, &#39;%d-%d-%d&#39;);
      if (!checkdate($month, $day, $year)) {
        $this->_error(self::INVALID_DATE);
        return false;
      }
    }
    return true;
}

InArray陣列包含校驗器

如果內(nèi)容包含在陣列中將傳回True,否則傳回False。

代碼:

<?php
require_once &#39;Zend/Validate/InArray.php&#39;;
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)容:重慶,存在於指定陣列中!

指定的內(nèi)容:鄭州,不存在於指定陣列中!

Regex正規(guī)符合校驗器

透過使用正規(guī)表示式,再加上合理使用本校驗器,幾乎可以實現(xià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ī)則!

點評:

public function __construct($pattern)
{
    if ($pattern instanceof Zend_Config) {
      $pattern = $pattern->toArray();
    }
    if (is_array($pattern)) {
      if (array_key_exists(&#39;pattern&#39;, $pattern)) {
        $pattern = $pattern[&#39;pattern&#39;];
      } else {
        require_once &#39;Zend/Validate/Exception.php&#39;;
        throw new Zend_Validate_Exception("Missing option &#39;pattern&#39;");
      }
    }
    $this->setPattern($pattern);
}

建構(gòu)函式初始化私有屬性,

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)行驗證工作。

自訂校驗器編寫

繼承Zend_Validate_Interface介面實作使用者自訂校驗器。

程式碼案例,功能判斷指定數(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();
}

要實現(xiàn)其中的兩個方法,一個是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[] = "&#39;$value&#39;不能被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ù)值:

rrreee

結(jié)果:

指定的數(shù)值:

3的數(shù)值!

失敗的消息是:

'5'不能被3整除

指定的數(shù)值:6,是3的倍數(shù)!

指定的數(shù)值:8,不是3的倍數(shù)!

失敗的訊息為:

'8'不能被3整除

點評:

這裡透過isValid()方法來設(shè)定屬性訊息,透過getMessages()方法來取得錯誤訊息。錯誤訊息是一個數(shù)組,透過foreach()方法來遍歷讀取。 ????希望本文所述對大家基於Zend Framework框架的PHP程式設(shè)計有所幫助。 ????更多Zend Framework常用校驗器詳解相關(guān)文章請關(guān)注PHP中文網(wǎng)! ??
本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1597
29
PHP教程
1488
72