Zend_Validate-ValidatorChains.xml 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.validate.validator_chains">
  4. <title>Validator Chains</title>
  5. <para>
  6. Often multiple validations should be applied to some value in a particular order. The
  7. following code demonstrates a way to solve the example from the
  8. <link linkend="zend.validate.introduction">introduction</link>, where a username must be
  9. between 6 and 12 alphanumeric characters:
  10. <programlisting language="php"><![CDATA[
  11. // Create a validator chain and add validators to it
  12. $validatorChain = new Zend_Validate();
  13. $validatorChain->addValidator(
  14. new Zend_Validate_StringLength(array('min' => 6,
  15. 'max' => 12)))
  16. ->addValidator(new Zend_Validate_Alnum());
  17. // Validate the username
  18. if ($validatorChain->isValid($username)) {
  19. // username passed validation
  20. } else {
  21. // username failed validation; print reasons
  22. foreach ($validatorChain->getMessages() as $message) {
  23. echo "$message\n";
  24. }
  25. }
  26. ]]></programlisting>
  27. Validators are run in the order they were added to <classname>Zend_Validate</classname>. In
  28. the above example, the username is first checked to ensure that its length is between 6 and
  29. 12 characters, and then it is checked to ensure that it contains only alphanumeric
  30. characters. The second validation, for alphanumeric characters, is performed regardless of
  31. whether the first validation, for length between 6 and 12 characters, succeeds. This means
  32. that if both validations fail, <methodname>getMessages()</methodname> will return failure
  33. messages from both validators.
  34. </para>
  35. <para>
  36. In some cases it makes sense to have a validator break the chain if its validation process
  37. fails. <classname>Zend_Validate</classname> supports such use cases with the second
  38. parameter to the <methodname>addValidator()</methodname> method. By setting
  39. <varname>$breakChainOnFailure</varname> to <constant>TRUE</constant>, the added validator
  40. will break the chain execution upon failure, which avoids running any other validations that
  41. are determined to be unnecessary or inappropriate for the situation. If the above example
  42. were written as follows, then the alphanumeric validation would not occur if the string
  43. length validation fails:
  44. <programlisting language="php"><![CDATA[
  45. $validatorChain->addValidator(
  46. new Zend_Validate_StringLength(array('min' => 6,
  47. 'max' => 12)),
  48. true)
  49. ->addValidator(new Zend_Validate_Alnum());
  50. ]]></programlisting>
  51. </para>
  52. <para>
  53. Any object that implements <classname>Zend_Validate_Interface</classname> may be used in a
  54. validator chain.
  55. </para>
  56. </sect1>
  57. <!--
  58. vim:se ts=4 sw=4 et:
  59. -->