Use of ZF framework validator [custom validator and validator chain]

WBOY
Release: 2016-07-25 08:48:40
Original
898 people have browsed it
homework exercises
  1. require_once("Zend/Loader.php");
  2. //Introduce the validator class and the functional class of the validator (Int), and the custom interface class;
  3. Zend_Loader::loadClass( 'Zend_Validate');
  4. Zend_Loader::loadClass('Zend_Validate_Int');
  5. Zend_Loader::loadClass('Zend_Validate_Interface');
  6. //Add custom validator function class (GongBeiNum) [common multiple]
  7. Class GongBeiNum implements Zend_Validate_Interface
  8. {
  9. //Declare the error message reporting attribute in the interface
  10. protected $_messages = array();
  11. //Declare the verification method in the interface
  12. public function isValid($num)
  13. {
  14. if (!($num%3 ==0) && !($num%5==0))
  15. {
  16. //If the verification fails, the error message return value is given to the error message report attribute
  17. $this -> _messages[] = "Your message The entered value is not a common multiple of 3 and 5! ";
  18. // Terminate the program
  19. return false;
  20. }
  21. // Return true
  22. return true;
  23. }
  24. // Define the error reporting method of the interface
  25. public function getMessages()
  26. {
  27. return $this -> _messages;
  28. }
  29. //Define extraction error messages (optional)
  30. public function getErrors()
  31. {
  32. }
  33. }
  34. //Out-of-class definition of common multiple detection method
  35. function check_num($num )
  36. {
  37. //Instantiate the validator class
  38. $Validate = new Zend_Validate();
  39. //Add a validator function class, add a custom validator function class, and form a validator chain
  40. $Validate - > addValidator(new Zend_Validate_Int())
  41. -> addValidator(new GongBeiNum());
  42. //Validate parameters
  43. if (!$Validate -> isValid($num))
  44. {
  45. //Loop if error Error message and output
  46. foreach ($Validate -> getMessages() as $value)
  47. {
  48. echo $value . "
    ";
  49. return false;
  50. }
  51. }
  52. }
  53. //Specify the judgment Value
  54. $num1 = '15';
  55. //Run the check method
  56. check_num($num1);
  57. ?>
Copy code


source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template