Use of ZF framework validator [custom validator and validator chain]
Release: 2016-07-25 08:48:40
Original
876 people have browsed it
homework exercises
- require_once("Zend/Loader.php");
- //Introduce the validator class and the functional class of the validator (Int), 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 function class (GongBeiNum) [common multiple]
- Class GongBeiNum implements Zend_Validate_Interface
- {
- //Declare the error message reporting 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 return value is given to the error message report attribute
- $this -> _messages[] = "Your message The entered value is not a common multiple of 3 and 5! ";
- // Terminate the program
- return false;
- }
- // Return true
- return true;
- }
- // Define the error reporting method of the interface
- public function getMessages()
- {
- return $this -> _messages;
- }
- //Define extraction error messages (optional)
- public function getErrors()
- {
-
- }
- }
- //Out-of-class definition of common multiple detection method
- function check_num($num )
- {
- //Instantiate the validator class
- $Validate = new Zend_Validate();
- //Add a validator function class, add a custom validator function class, and form a validator chain
- $Validate - > addValidator(new Zend_Validate_Int())
- -> addValidator(new GongBeiNum());
- //Validate parameters
- if (!$Validate -> isValid($num))
- {
- //Loop if error Error message and output
- foreach ($Validate -> getMessages() as $value)
- {
- echo $value . "
";
- return false;
- }
- }
- }
-
- //Specify the judgment Value
- $num1 = '15';
- //Run the check method
- check_num($num1);
- ?>
Copy code
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31