Zend_Validate-Messages.xml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 24249 -->
  4. <sect1 id="zend.validate.messages">
  5. <title>検証メッセージ(一部日本語)</title>
  6. <para>
  7. <classname>Zend_Validate</classname> を継承したバリデータには、
  8. 検証に失敗したときに使用するメッセージが用意されています。
  9. <!-- TODO : to be translated -->
  10. You can use this information to set your own messages,
  11. or to translate existing messages which a validator could return to something different.
  12. </para>
  13. <para>
  14. These validation messages are constants which can be found at top of each validator class.
  15. Let's look into <classname>Zend_Validate_GreaterThan</classname> for an descriptive example:
  16. </para>
  17. <programlisting language="php"><![CDATA[
  18. protected $_messageTemplates = array(
  19. self::NOT_GREATER => "'%value%' is not greater than '%min%'",
  20. );
  21. ]]></programlisting>
  22. <para>
  23. As you can see the constant <constant>self::NOT_GREATER</constant> refers to the failure and
  24. is used as key, and the message itself is used as value of the message array.
  25. </para>
  26. <para>
  27. You can retrieve all message templates from a validator by using the
  28. <methodname>getMessageTemplates()</methodname> method. It returns you the above array which
  29. contains all messages a validator could return in the case of a failed validation.
  30. </para>
  31. <programlisting language="php"><![CDATA[
  32. $validator = new Zend_Validate_GreaterThan();
  33. $messages = $validator->getMessageTemplates();
  34. ]]></programlisting>
  35. <para>
  36. Using the <methodname>setMessage()</methodname> method you can set another message to be
  37. returned in case of the specified failure.
  38. </para>
  39. <programlisting language="php"><![CDATA[
  40. $validator = new Zend_Validate_GreaterThan();
  41. $validator->setMessage(
  42. 'Please enter a lower value',
  43. Zend_Validate_GreaterThan::NOT_GREATER
  44. );
  45. ]]></programlisting>
  46. <para>
  47. The second parameter defines the failure which will be overridden. When you omit this
  48. parameter, then the given message will be set for all possible failures of this validator.
  49. </para>
  50. <sect2 id="zend.validate.messages.pretranslated">
  51. <title>Using pre-translated validation messages</title>
  52. <para>
  53. Zend Framework is shipped with more than 45 different validators with more than 200
  54. failure messages. It can be a tedious task to translate all of these messages. But for
  55. your convenience Zend Framework comes with already pre-translated validation messages.
  56. You can find them within the path <filename>/resources/languages</filename> in your
  57. Zend Framework installation.
  58. </para>
  59. <note>
  60. <title>Used path</title>
  61. <para>
  62. The resource files are outside of the library path because all of your translations
  63. should also be outside of this path.
  64. </para>
  65. </note>
  66. <para>
  67. So to translate all validation messages to German for example, all you have to do is to
  68. attach a translator to <classname>Zend_Validate</classname> using these resource files.
  69. </para>
  70. <programlisting language="php"><![CDATA[
  71. $translator = new Zend_Translate(
  72. array(
  73. 'adapter' => 'array',
  74. 'content' => '/resources/languages',
  75. 'locale' => $language,
  76. 'scan' => Zend_Translate::LOCALE_DIRECTORY
  77. )
  78. );
  79. Zend_Validate_Abstract::setDefaultTranslator($translator);
  80. ]]></programlisting>
  81. <note>
  82. <title>Used translation adapter</title>
  83. <para>
  84. As translation adapter Zend Framework chose the array adapter. It is simple to
  85. edit and created very fast.
  86. </para>
  87. </note>
  88. <note>
  89. <title>Supported languages</title>
  90. <para>
  91. This feature is very young, so the amount of supported languages may not be
  92. complete. New languages will be added with each release. Additionally feel free to
  93. use the existing resource files to make your own translations.
  94. </para>
  95. <para>
  96. You could also use these resource files to rewrite existing translations. So you
  97. are not in need to create these files manually yourself.
  98. </para>
  99. </note>
  100. </sect2>
  101. <sect2 id="zend.validate.messages.limitation">
  102. <title>検証メッセージのサイズの制限</title>
  103. <para>
  104. 検証メッセージの最大サイズを制限しなければならないこともあるでしょう。
  105. たとえば、1 行に 100 文字までしかレンダリングできないなどの制限がビューにある場合です。
  106. このような場合のため、<classname>Zend_Validate</classname>
  107. では自動的に検証メッセージの最大長を制限できるようになっています。
  108. </para>
  109. <para>
  110. 実際に設定されているサイズを取得するには
  111. <methodname>Zend_Validate::getMessageLength()</methodname> を使用します。
  112. この結果が -1 の場合は、返されるメッセージが切り詰められることはありません。
  113. これがデフォルトの挙動です。
  114. </para>
  115. <para>
  116. 返されるメッセージのサイズを制限するには
  117. <methodname>Zend_Validate::setMessageLength()</methodname> を使用します。
  118. 必要に応じて任意の整数値を設定します。
  119. 返されるメッセージのサイズがここで設定した長さを超えると、
  120. メッセージが切り詰められて最後に文字列 '<emphasis>...</emphasis>'
  121. が付加されます。
  122. </para>
  123. <programlisting language="php"><![CDATA[
  124. Zend_Validate::setMessageLength(100);
  125. ]]></programlisting>
  126. <note>
  127. <title>このパラメータはどこで使われますか?</title>
  128. <para>
  129. ここで設定したメッセージ長はすべてのバリデータで使われます。
  130. 自前で定義したバリデータでさえも、それが <classname>Zend_Validate_Abstract</classname>
  131. を継承したものである限りは同じです。
  132. </para>
  133. </note>
  134. </sect2>
  135. </sect1>
  136. <!--
  137. vim:se ts=4 sw=4 et:
  138. -->