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

Source: Internet
Author: User
Use of the ZF framework Validator [custom validator and validator chain]
Homework

  1. Require_once ("Zend/Loader. php ");
  2. // Introduce the validator class and the validator function class (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. // Added the UDF class GongBeiNum [public Multiple]
  7. Class GongBeiNum implements Zend_Validate_Interface
  8. {
  9. // Declare the error message report 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 returned value is used to report the attribute to the error message.
  17. $ This-> _ messages [] = "the value you entered is not a public multiple of 3 and 5! ";
  18. // Terminate the program
  19. Return false;
  20. }
  21. // Returns true
  22. Return true;
  23. }
  24. // Define the interface error reporting method
  25. Public function getMessages ()
  26. {
  27. Return $ this-> _ messages;
  28. }
  29. // Define the extraction error message (optional)
  30. Public function getErrors ()
  31. {
  32. }
  33. }
  34. // Out-of-class definition detection public multiples method
  35. Function check_num ($ num)
  36. {
  37. // Instantiate the validator class
  38. $ Validate = new Zend_Validate ();
  39. // Add the validator function class and the custom validator function class to form the validator chain
  40. $ Validate-> addValidator (new Zend_Validate_Int ())
  41. -> AddValidator (new GongBeiNum ());
  42. // Check parameters
  43. If (! $ Validate-> isValid ($ num ))
  44. {
  45. // If the error loop error message is returned
  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 verification method
  56. Check_num ($ num1 );
  57. ?>

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.