1 <?xml version="1.0" encoding="UTF-8"?>
3 <!-- EN-Revision: 21661 -->
4 <sect1 id="zend.validate.messages">
9 <classname>Zend_Validate</classname> を継承したバリデータには、
10 検証に失敗したときに使用するメッセージが用意されています。
11 You can use this information to set your own messages,
12 or to translate existing messages which a validator could return to something different.
16 These validation messages are constants which can be found at top of each validator class.
17 Let's look into <classname>Zend_Validate_GreaterThan</classname> for an descriptive example:
20 <programlisting language="php"><![CDATA[
21 protected $_messageTemplates = array(
22 self::NOT_GREATER => "'%value%' is not greater than '%min%'",
27 As you can see the constant <constant>self::NOT_GREATER</constant> refers to the failure and
28 is used as key, and the message itself is used as value of the message array.
32 You can retrieve all message templates from a validator by using the
33 <methodname>getMessageTemplates()</methodname> method. It returns you the above array which
34 contains all messages a validator could return in the case of a failed validation.
37 <programlisting language="php"><![CDATA[
38 $validator = new Zend_Validate_GreaterThan();
39 $messages = $validator->getMessageTemplates();
43 Using the <methodname>setMessage()</methodname> method you can set another message to be
44 returned in case of the specified failure.
47 <programlisting language="php"><![CDATA[
48 $validator = new Zend_Validate_GreaterThan();
49 $validator->setMessage('Please enter a lower value', Zend_Validate_GreaterThan::NOT_GREATER);
53 The second parameter defines the failure which will be overridden. When you omit this
54 parameter, then the given message will be set for all possible failures of this validator.
57 <sect2 id="zend.validate.messages.pretranslated">
58 <title>Using pre-translated validation messages</title>
61 Zend Framework is shipped with more than 45 different validators with more than 200
62 failure messages. It can be a tedious task to translate all of these messages. But for
63 your convenience Zend Framework comes with already pre-translated validation messages.
64 You can find them within the path <filename>/resources/languages</filename> in your
65 Zend Framework installation.
69 <title>Used path</title>
72 The resource files are outside of the library path because all of your translations
73 should also be outside of this path.
78 So to translate all validation messages to German for example, all you have to do is to
79 attach a translator to <classname>Zend_Validate</classname> using these resource files.
82 <programlisting language="php"><![CDATA[
83 $translator = new Zend_Translate(
86 'content' => '/resources/languages',
87 'locale' => $language,
88 'scan' => Zend_Translate::LOCALE_DIRECTORY
91 Zend_Validate_Abstract::setDefaultTranslator($translator);
95 <title>Used translation adapter</title>
98 As translation adapter Zend Framework chose the array adapter. It is simple to
99 edit and created very fast.
104 <title>Supported languages</title>
107 This feature is very young, so the amount of supported languages may not be
108 complete. New languages will be added with each release. Additionally feel free to
109 use the existing resource files to make your own translations.
113 You could also use these resource files to rewrite existing translations. So you
114 are not in need to create these files manually yourself.
119 <sect2 id="zend.validate.messages.limitation">
120 <title>検証メッセージのサイズの制限</title>
123 検証メッセージの最大サイズを制限しなければならないこともあるでしょう。
124 たとえば、1 行に 100 文字までしかレンダリングできないなどの制限がビューにある場合です。
125 このような場合のため、<classname>Zend_Validate</classname>
126 では自動的に検証メッセージの最大長を制限できるようになっています。
131 <methodname>Zend_Validate::getMessageLength()</methodname> を使用します。
132 この結果が -1 の場合は、返されるメッセージが切り詰められることはありません。
138 <methodname>Zend_Validate::setMessageLength()</methodname> を使用します。
140 返されるメッセージのサイズがここで設定した長さを超えると、
141 メッセージが切り詰められて最後に文字列 '<emphasis>...</emphasis>'
145 <programlisting language="php"><![CDATA[
146 Zend_Validate::setMessageLength(100);
150 <title>このパラメータはどこで使われますか?</title>
153 ここで設定したメッセージ長はすべてのバリデータで使われます。
154 自前で定義したバリデータでさえも、それが <classname>Zend_Validate_Abstract</classname>