The ZF framework uses an example of a validator

Source: Internet
Author: User
Tags class definition

  This article mainly introduces the ZF framework for use with the validator (custom calibrator and validator chain), need friends can refer to the following

  Code as follows: <?php require_once ("zend/loader.php"); The introduction of the validator class and the function Class (INT) of the calibrator, and the custom interface class; Zend_loader::loadclass (' zend_validate '); Zend_loader::loadclass (' Zend_validate_int '); Zend_loader::loadclass (' Zend_validate_interface '); Add custom Validator Feature Class (gongbeinum) [Common multiple] class Gongbeinum implements Zend_validate_interface { //Declaration interface error message Reporting properties   Protected $_messages = Array ();  //the validation method in the interface  public function IsValid ($num)  {  if (! $num%3==0) &&! ($num%5==0))     {   //if the checksum fails, the error message returns the value to the error message report properties     $this-> _messages[] = "The number you entered is not a common multiple of 3 and 5!" ";    //terminating procedure    return false;  }  //Back true   return true; &NBSP}  //The error method that defines the interface  public function getmessages ()  {  return $this-> _messages;  }  / /Definition Fetch error message (can not)  public function geterrors ()  {  &NBSP}}//class definition detection Common multiple method function Check_num ($num) { // Instantiate Validator class   $Validate = new Zend_validate ();  //Add Validator Feature class, add customThe function class of the validator, form the calibrator chain   $Validate-> addvalidator (New Zend_validate_int ())      -> Addvalidator (new Gongbeinum ());  //Check parameter  if (! $Validate-> isValid ($num))    { //If error loop error message and output    foreach ($ Validate-> getmessages () as $value)      {    echo $value. "<br>";     return false;   &NBSP} &nbsp}  //Specify the value of the judgment $num 1 = ' 15 '; Run Check method Check_num ($num 1);?>  

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.