| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- EN-Revision: 20352 -->
- <!-- Reviewed: no -->
- <sect1 id="zend.validate.messages">
- <title>Messages de validation</title>
- <para>
- Chaque validateur basé sur <classname>Zend_Validate</classname> propose un ou plusieurs messages
- dans le cas d'un echec. Vous pouvez utiliser ces informations pour créer vos propres messages
- ou pour traduire les messages présents.
- </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 tendious task to translate all of these messages. But for
- your convinience 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',
- '/resources/languages',
- $language,
- array('scan' => Zend_Locale::LOCALE_DIRECTORY)
- );
- Zend_Validate_Abstract::setDefaultTranslator($translator);
- ]]></programlisting>
- <note>
- <title>Used translation adapter</title>
- <para>
- As translation adapter Zend Framework choosed 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>Limiter la taille d'un message de validation</title>
- <para>
- Il peut être nécessaire parfois de limiter la taille en caractères des messages d'erreur
- retournés. par exemple si une vue n'autorise que 100 caractères par ligne.
- <classname>Zend_Validate</classname> propose une telle option.
- </para>
- <para>
- La taille actuelle est
- <methodname>Zend_Validate::getMessageLength()</methodname>. -1 signifie que le message ne
- sera pas tronqué et entièrement retourné, c'est le comportement par défaut.
- </para>
- <para>
- Pour limiter la taille, utilisez
- <methodname>Zend_Validate::setMessageLength()</methodname>. Lorsque la taille excède cette valeur,
- le message sera alors tronqué et suivi de '<emphasis>...</emphasis>'.
- </para>
- <programlisting language="php"><![CDATA[
- Zend_Validate::setMessageLength(100);
- ]]></programlisting>
- <note>
- <title>Où ce paramètre est-il utilisé ?</title>
- <para>
- La taille des messages affecte aussi les messages personnalisés enregistrés, dès
- que le validateur considéré étend <classname>Zend_Validate_Abstract</classname>.
- </para>
- </note>
- </sect2>
- </sect1>
|