Zend_Validate-Messages.xml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.validate.messages">
  4. <title>Validation Messages</title>
  5. <para>
  6. Each validator which is based on <classname>Zend_Validate</classname> provides
  7. one or multiple messages in the case of a failed validation. You can use
  8. this information to set your own messages, or to translate existing messages which a
  9. validator could return to something different.
  10. </para>
  11. <para>
  12. These validation messages are constants which can be found at top of each validator class.
  13. Let's look into <classname>Zend_Validate_GreaterThan</classname> for an descriptive example:
  14. </para>
  15. <programlisting language="php"><![CDATA[
  16. protected $_messageTemplates = array(
  17. self::NOT_GREATER => "'%value%' is not greater than '%min%'",
  18. );
  19. ]]></programlisting>
  20. <para>
  21. As you can see the constant <constant>self::NOT_GREATER</constant> refers to the failure and
  22. is used as key, and the message itself is used as value of the message array.
  23. </para>
  24. <para>
  25. You can retrieve all message templates from a validator by using the
  26. <methodname>getMessageTemplates()</methodname> method. It returns you the above array which
  27. contains all messages a validator could return in the case of a failed validation.
  28. </para>
  29. <programlisting language="php"><![CDATA[
  30. $validator = new Zend_Validate_GreaterThan();
  31. $messages = $validator->getMessageTemplates();
  32. ]]></programlisting>
  33. <para>
  34. Using the <methodname>setMessage()</methodname> method you can set another message to be
  35. returned in case of the specified failure.
  36. </para>
  37. <programlisting language="php"><![CDATA[
  38. $validator = new Zend_Validate_GreaterThan();
  39. $validator->setMessage(
  40. 'Please enter a lower value',
  41. Zend_Validate_GreaterThan::NOT_GREATER
  42. );
  43. ]]></programlisting>
  44. <para>
  45. The second parameter defines the failure which will be overridden. When you omit this
  46. parameter, then the given message will be set for all possible failures of this validator.
  47. </para>
  48. <sect2 id="zend.validate.messages.pretranslated">
  49. <title>Using pre-translated validation messages</title>
  50. <para>
  51. Zend Framework is shipped with more than 45 different validators with more than 200
  52. failure messages. It can be a tedious task to translate all of these messages. But for
  53. your convenience Zend Framework comes with already pre-translated validation messages.
  54. You can find them within the path <filename>/resources/languages</filename> in your
  55. Zend Framework installation.
  56. </para>
  57. <note>
  58. <title>Used path</title>
  59. <para>
  60. The resource files are outside of the library path because all of your translations
  61. should also be outside of this path.
  62. </para>
  63. </note>
  64. <para>
  65. So to translate all validation messages to German for example, all you have to do is to
  66. attach a translator to <classname>Zend_Validate</classname> using these resource files.
  67. </para>
  68. <programlisting language="php"><![CDATA[
  69. $translator = new Zend_Translate(
  70. array(
  71. 'adapter' => 'array',
  72. 'content' => '/resources/languages',
  73. 'locale' => $language,
  74. 'scan' => Zend_Translate::LOCALE_DIRECTORY
  75. )
  76. );
  77. Zend_Validate_Abstract::setDefaultTranslator($translator);
  78. ]]></programlisting>
  79. <note>
  80. <title>Used translation adapter</title>
  81. <para>
  82. As translation adapter Zend Framework chose the array adapter. It is simple to
  83. edit and created very fast.
  84. </para>
  85. </note>
  86. <note>
  87. <title>Supported languages</title>
  88. <para>
  89. This feature is very young, so the amount of supported languages may not be
  90. complete. New languages will be added with each release. Additionally feel free to
  91. use the existing resource files to make your own translations.
  92. </para>
  93. <para>
  94. You could also use these resource files to rewrite existing translations. So you
  95. are not in need to create these files manually yourself.
  96. </para>
  97. </note>
  98. </sect2>
  99. <sect2 id="zend.validate.messages.limitation">
  100. <title>Limit the size of a validation message</title>
  101. <para>
  102. Sometimes it is necessary to limit the maximum size a validation message can have.
  103. For example when your view allows a maximum size of 100 chars to be rendered on one
  104. line. To simplify the usage, <classname>Zend_Validate</classname> is able to
  105. automatically limit the maximum returned size of a validation message.
  106. </para>
  107. <para>
  108. To get the actual set size use
  109. <methodname>Zend_Validate::getMessageLength()</methodname>. If it is -1, then the
  110. returned message will not be truncated. This is default behaviour.
  111. </para>
  112. <para>
  113. To limit the returned message size use
  114. <methodname>Zend_Validate::setMessageLength()</methodname>. Set it to any integer size
  115. you need. When the returned message exceeds the set size, then the message
  116. will be truncated and the string '<emphasis>...</emphasis>' will be added instead of
  117. the rest of the message.
  118. </para>
  119. <programlisting language="php"><![CDATA[
  120. Zend_Validate::setMessageLength(100);
  121. ]]></programlisting>
  122. <note>
  123. <title>Where is this parameter used?</title>
  124. <para>
  125. The set message length is used for all validators, even for self defined ones,
  126. as long as they extend <classname>Zend_Validate_Abstract</classname>.
  127. </para>
  128. </note>
  129. </sect2>
  130. </sect1>
  131. <!--
  132. vim:se ts=4 sw=4 et:
  133. -->