The ZF Framework Validator uses examples (custom validators and validator chains) _php instances

Source: Internet
Author: User
Tags class definition

Copy Code 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
{
Declaring an error message report property in an interface
Protected $_messages = Array ();
The calibration method in the Declaration 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 property
$this-> _messages[] = "The number you entered is not the common multiple of 3 and 5!" ";
Terminate program
return false;
}
return True
return true;
}
To define an error method for an interface
Public Function Getmessages ()
{
return $this-> _messages;
}
Define extraction Error message (none)
Public Function geterrors ()
{

}
}
Class definition detection Common multiple method
function Check_num ($num)
{
Instantiating the Validator class
$Validate = new Zend_validate ();
Add validator function class, add custom Validator function class, form validator chain
$Validate-> Addvalidator (New Zend_validate_int ())
-> Addvalidator (New Gongbeinum ());
Checksum parameters
if (! $Validate-> isValid ($num))
{
If error loops error message and outputs
foreach ($Validate-> getmessages () as $value)
{
Echo $value. "<br>";
return false;
}
}
}

Specify the value of the judgment
$num 1 = ' 15 ';
The method of verifying the running school
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.