File: Zend_Validate-Messages.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 (160 lines) | stat: -rw-r--r-- 6,068 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.validate.messages">
    <title>Validation Messages</title>

    <para>
        Each validator which is based on <classname>Zend_Validate</classname> provides
        one or multiple messages in the case of a failed validation. You can use
        this information to set your own messages, or to translate existing messages which a
        validator could return to something different.
    </para>

    <para>
        These validation messages are constants which can be found at top of each validator class.
        Let's look into <classname>Zend_Validate_GreaterThan</classname> for an descriptive example:
    </para>

    <programlisting language="php"><![CDATA[
protected $_messageTemplates = array(
    self::NOT_GREATER => "'%value%' is not greater than '%min%'",
);
]]></programlisting>

    <para>
        As you can see the constant <constant>self::NOT_GREATER</constant> refers to the failure and
        is used as key, and the message itself is used as value of the message array.
    </para>

    <para>
        You can retrieve all message templates from a validator by using the
        <methodname>getMessageTemplates()</methodname> method. It returns you the above array which
        contains all messages a validator could return in the case of a failed validation.
    </para>

    <programlisting language="php"><![CDATA[
$validator = new Zend_Validate_GreaterThan();
$messages  = $validator->getMessageTemplates();
]]></programlisting>

    <para>
        Using the <methodname>setMessage()</methodname> method you can set another message to be
        returned in case of the specified failure.
    </para>

    <programlisting language="php"><![CDATA[
$validator = new Zend_Validate_GreaterThan();
$validator->setMessage(
    'Please enter a lower value',
    Zend_Validate_GreaterThan::NOT_GREATER
);
]]></programlisting>

    <para>
        The second parameter defines the failure which will be overridden. When you omit this
        parameter, then the given message will be set for all possible failures of this validator.
    </para>

    <sect2 id="zend.validate.messages.pretranslated">
        <title>Using pre-translated validation messages</title>

        <para>
            Zend Framework is shipped with more than 45 different validators with more than 200
            failure messages. It can be a tedious task to translate all of these messages. But for
            your convenience Zend Framework comes with already pre-translated validation messages.
            You can find them within the path <filename>/resources/languages</filename> in your
            Zend Framework installation.
        </para>

        <note>
            <title>Used path</title>

            <para>
                The resource files are outside of the library path because all of your translations
                should also be outside of this path.
            </para>
        </note>

        <para>
            So to translate all validation messages to German for example, all you have to do is to
            attach a translator to <classname>Zend_Validate</classname> using these resource files.
        </para>

        <programlisting language="php"><![CDATA[
$translator = new Zend_Translate(
    array(
        'adapter' => 'array',
        'content' => '/resources/languages',
        'locale'  => $language,
        'scan' => Zend_Translate::LOCALE_DIRECTORY
    )
);
Zend_Validate_Abstract::setDefaultTranslator($translator);
]]></programlisting>

        <note>
            <title>Used translation adapter</title>

            <para>
                As translation adapter Zend Framework chose the array adapter. It is simple to
                edit and created very fast.
            </para>
        </note>

        <note>
            <title>Supported languages</title>

            <para>
                This feature is very young, so the amount of supported languages may not be
                complete. New languages will be added with each release. Additionally feel free to
                use the existing resource files to make your own translations.
            </para>

            <para>
                You could also use these resource files to rewrite existing translations. So you
                are not in need to create these files manually yourself.
            </para>
        </note>
    </sect2>

    <sect2 id="zend.validate.messages.limitation">
        <title>Limit the size of a validation message</title>

        <para>
            Sometimes it is necessary to limit the maximum size a validation message can have.
            For example when your view allows a maximum size of 100 chars to be rendered on one
            line. To simplify the usage, <classname>Zend_Validate</classname> is able to
            automatically limit the maximum returned size of a validation message.
        </para>

        <para>
            To get the actual set size use
            <methodname>Zend_Validate::getMessageLength()</methodname>. If it is -1, then the
            returned message will not be truncated. This is default behaviour.
        </para>

        <para>
            To limit the returned message size use
            <methodname>Zend_Validate::setMessageLength()</methodname>. Set it to any integer size
            you need. When the returned message exceeds the set size, then the message
            will be truncated and the string '<emphasis>...</emphasis>' will be added instead of
            the rest of the message.
        </para>

        <programlisting language="php"><![CDATA[
Zend_Validate::setMessageLength(100);
]]></programlisting>

        <note>
            <title>Where is this parameter used?</title>

            <para>
                The set message length is used for all validators, even for self defined ones,
                as long as they extend <classname>Zend_Validate_Abstract</classname>.
            </para>
        </note>
    </sect2>
</sect1>
<!--
vim:se ts=4 sw=4 et:
-->