Zend Framework Common Calibrator _php Example

Source: Internet
Author: User
Tags zend zend framework

The example of this article describes the common calibrator for the Zend Framework. Share to everyone for your reference, specific as follows:

Date Day Validator

Code:

<?php
require_once ' zend/validate/date.php ';
function C_date ($date) {
  $validator = new Zend_validate_date ();
  The date format entered by the IF ($validator->isvalid ($date)) {
    echo: ";
    echo $date. " Effective! <p> ";
  } else{
    echo "Enter the date format:";
    echo $date. " Invalid! <p> ";
  }
}
$date 1 = "2008-02-15";
$date 2 = "2008-02-31";
$date 3 = "02-15-2008";
C_date ($date 1);
C_date ($date 2);
C_date ($date 3);

Results:

Date format entered: 2008-02-15 effective!

Date format entered: 2008-02-31 invalid!

Date format entered: 02-15-2008 invalid!

Comments: Source 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

Returns true if the content is contained in the array, or false.

Code:

<?php
require_once ' zend/validate/inarray.php ';
function C_array ($n) {
  $temp = array ("Beijing", "Shanghai", "Tianjin", "Chongqing");
  $validator = new Zend_validate_inarray ($temp);
  if ($validator->isvalid ($n)) {
    echo "specifies:";
    echo $n. ", existing in the specified array! <p> ";
  } else{
    echo "The specified content:";
    echo $n. ", does not exist in the specified array! <p> ";
  }
}
$city 1 = "Beijing";
$city 2 = "Chongqing";
$city 3 = "Zhengzhou";
C_array ($city 1);
C_array ($city 2);
C_array ($city 3);

Results:

Content specified: Beijing, exists in the specified array!

Specified: Chongqing, which exists in the specified array!

Content specified: Zhengzhou, does not exist in the specified array!

Regex Regular Matching Checker

By using regular expressions, coupled with the rational use of the school's detector, almost all of the validation 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 \" > Specified content: ";
    echo $v. " <p> conform to the defined formal rules! </font> ";
    echo "<p>";
  } else{
    echo "<font color=\" #ff0000 \ "> Specified content:";
    echo $v. " <p> does not conform to the defined formal rules! </font> ";
    echo "<p>";
  }
}
$temp 1 = "AB";
$temp 2 = "ABB";
$temp 3 = "ABBB";
C_rege ($temp 1);
C_rege ($temp 2);
C_rege ($temp 3);

Results:

Content specified: AB

Formal rules that do not conform to the definition!

Content specified: ABB

Conform to the formal rules of the definition!

Content specified: ABBB

Conform to the formal rules of the definition!

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);
}

constructor to initialize the private property,

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;
}

Perform validation work.

Custom Validator Authoring

Inherits the Zend_validate_interface interface to implement a user-defined validator.

A code case that functions to determine whether a specified number is a multiple of 3.

Interface code:

<?php/** * Zend Framework * * * LICENSE * This source file was subject to the new BSD LICENSE-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 Li
 cense@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://w ww.zend.com) * @license HTTP://FRAMEWORK.ZEND.COM/LICENSE/NEW-BSD new BSD license * @version $Id: interface.php 2459 3 2012-01-05 20:35:02z Matthew $//** * @category Zend * @package zend_validate * @copyright Copyright (c) 2005-20 Zend Technologies USA INC (http://www.zend.com) * @license Http://framework.zend.com/license/new-bsd New BSD Licen SE */interface Zend_validate_interface {/** Returns true if and only if $value meets the validation requirements * * If $value fails validation, then thi S method returns False, and * getmessages () 'll return to an array of messages that explain why the * validation faile d. * * @param mixed $value * @return Boolean * @throws zend_validate_exception If Validation of $value is Impo
  ssible */Public Function isValid ($value); /** * Returns An array of messages this explain why the most recent () * Call IsValid false. The array keys are validation failure message identifiers, * and the array values are the corresponding human-readable
   Message strings. * * If IsValid () is never called or If the most recent isValid () call * returned true, then this method returns
   Empty array.
* * @return Array/Public function getmessages ();

 }

To implement two of these methods, one is IsValid () and one 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 ' cannot be divisible by 3"
      ; return false;
    }
    return true;
  }
  Public Function Getmessages () {return
    $this->_messages;
  }
}
function C_n_3 ($n) {
  $validator = new Myvalidator ();
  if ($validator->isvalid ($n)) {
    echo Specifies the value: ";
    echo $n. ", is a multiple of 3!" <p> ";
  } else{
    echo "specified value:";
    echo $n. ", not a multiple of 3!" <p> ";
    echo "failed message is:<p>";
    foreach ($validator->getmessages () as $message) {
      echo $message <p>
}}} $num 1 = 5;
$num 2 = 6;
$num 3 = 8;
C_n_3 ($num 1);
C_n_3 ($num 2);
C_n_3 ($num 3);

Results:

The specified value: 5, not a multiple of 3!

The failed message is:

' 5 ' cannot be divisible by 3

The specified value: 6, is a multiple of 3!

The specified value: 8, not a multiple of 3!

The failed message is:

' 8 ' cannot be divisible by 3

Comments:

Here, the IsValid () method is used to set the property information, and the Getmessages () method is used to get the error message. An error message is an array that traverses the read by the foreach () method.

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend Framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.