File: Zend_Validate-ValidatorChains.xml

package info (click to toggle)
zendframework 1.12.9%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 133,584 kB
  • sloc: xml: 1,311,829; php: 570,173; sh: 170; makefile: 125; sql: 121
file content (68 lines) | stat: -rw-r--r-- 3,007 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.validate.validator_chains">
    <title>Validator Chains</title>

    <para>
        Often multiple validations should be applied to some value in a particular order. The
        following code demonstrates a way to solve the example from the
        <link linkend="zend.validate.introduction">introduction</link>, where a username must be
        between 6 and 12 alphanumeric characters:
    </para>

    <programlisting language="php"><![CDATA[
// Create a validator chain and add validators to it
$validatorChain = new Zend_Validate();
$validatorChain->addValidator(
                    new Zend_Validate_StringLength(array('min' => 6,
                                                         'max' => 12)))
               ->addValidator(new Zend_Validate_Alnum());

// Validate the username
if ($validatorChain->isValid($username)) {
    // username passed validation
} else {
    // username failed validation; print reasons
    foreach ($validatorChain->getMessages() as $message) {
        echo "$message\n";
    }
}
]]></programlisting>

    <para>
        Validators are run in the order they were added to <classname>Zend_Validate</classname>. In
        the above example, the username is first checked to ensure that its length is between 6 and
        12 characters, and then it is checked to ensure that it contains only alphanumeric
        characters. The second validation, for alphanumeric characters, is performed regardless of
        whether the first validation, for length between 6 and 12 characters, succeeds. This means
        that if both validations fail, <methodname>getMessages()</methodname> will return failure
        messages from both validators.
    </para>

    <para>
        In some cases it makes sense to have a validator break the chain if its validation process
        fails. <classname>Zend_Validate</classname> supports such use cases with the second
        parameter to the <methodname>addValidator()</methodname> method. By setting
        <varname>$breakChainOnFailure</varname> to <constant>TRUE</constant>, the added validator
        will break the chain execution upon failure, which avoids running any other validations that
        are determined to be unnecessary or inappropriate for the situation. If the above example
        were written as follows, then the alphanumeric validation would not occur if the string
        length validation fails:
    </para>

    <programlisting language="php"><![CDATA[
$validatorChain->addValidator(
                    new Zend_Validate_StringLength(array('min' => 6,
                                                         'max' => 12)),
                    true)
               ->addValidator(new Zend_Validate_Alnum());
]]></programlisting>

    <para>
        Any object that implements <classname>Zend_Validate_Interface</classname> may be used in a
        validator chain.
    </para>
</sect1>
<!--
vim:se ts=4 sw=4 et:
-->