Use of the ZF framework Validator [custom validator and validator chain]
Homework
- 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 );
- ?>
|