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