Examples of using the zf framework validator (custom validator and validator chain)

Source: Internet
Author: User
This article describes how to use the zf framework validator (user-defined validator and validator chain). For more information, see The code is as follows:
Require_once ("Zend/Loader. php ");
// Introduce the validator class and the validator function class (Int), and the custom interface class;
Zend_Loader: loadClass ('zend _ validate ');
Zend_Loader: loadClass ('zend _ Validate_Int ');
Zend_Loader: loadClass ('zend _ Validate_Interface ');
// Added the UDF class GongBeiNum [public Multiple]
Class GongBeiNum implements Zend_Validate_Interface
{
// Declare the error message report attribute in the interface
Protected $ _ messages = array ();
// Declare the verification method in the interface
Public function isValid ($ num)
{
If (! ($ Num % 3 = 0 )&&! ($ Num % 5 = 0 ))
{
// If the verification fails, the error message returned value is used to report the attribute to the error message.
$ This-> _ messages [] = "the value you entered is not a public multiple of 3 and 5! ";
// Terminate the program
Return false;
}
// Returns true
Return true;
}
// Define the interface error reporting method
Public function getMessages ()
{
Return $ this-> _ messages;
}
// Define the extraction error message (optional)
Public function getErrors ()
{

}
}
// Out-of-class definition detection public multiples method
Function check_num ($ num)
{
// Instantiate the validator class
$ Validate = new Zend_Validate ();
// Add the validator function class and the custom validator function class to form the validator chain
$ Validate-> addValidator (new Zend_Validate_Int ())
-> AddValidator (new GongBeiNum ());
// Check parameters
If (! $ Validate-> isValid ($ num ))
{
// If the error loop error message is returned
Foreach ($ Validate-> getMessages () as $ value)
{
Echo $ value ."
";
Return false;
}
}
}

// Specify the judgment value
$ Num1 = '15 ';
// Run the verification method
Check_num ($ num1 );
?>

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.