-- Łukasz Kazimierz Bandzarewicz <[EMAIL PROTECTED]> wrote
(on Tuesday, 29 January 2008, 06:58 PM +0100):
> Ho to add custom validation messages in Zend_From?
> 
> For instance in standard method (without Zend_Form) I can do:
> 
> $validators = array(
>   'login' => array(
>     new Zend_Validate_NotEmpty(),
>     'messages' => array(
>       array(
>        // here is my custom message
>        Zend_Validate_NotEmpty::IS_EMPTY => 'Prosz  poda  login'
>       )
>     )
>   )....
> 
> $inputFilter = new Zend_Filter_Input($filters, $validators);
> $inputFilter->setDefaultEscapeFilter(new Zend_Filter_StringTrim());
> $inputFilter->setData($data);
> 
> if ($inputFilter->isValid()) {...

Zend_Form does not use Zend_Filter_Input, as it uses a separate
validator chain per element (ZFI operates on sets of elements), so the
mechanisms are different.

Currently, you *can* pull your validator object and set the messages:

    $notEmpty = $element->getValidator('NotEmpty');
    $notEmpty->setMessages($messages);

Additionally, if you are using Zend_Translate and pass a
Zend_Translate_Adapter into your form or element, the Errors decorator
will use the error codes as message keys for translation.

> This method works very good for me, but how to add custom validation messages,
> for example in this part of code:
> http://framework.zend.com/manual/en/zend.form.elements.html#
> zend.form.elements.validators ?
> 
> I suppose that I should use $options variable (from method Zend_Form_Element
> addValidator (string|Zend_Validate_Interface $validator,
> [bool $breakChainOnFailure = false], [array $options = array()]) ), but how?

This won't work currently, but I've marked it in my TODO as an an
improvement. It will work by passing a 'messages' key to the validator
options, with a value of an associative array of code/message pairs.

-- 
Matthew Weier O'Phinney
PHP Developer            | [EMAIL PROTECTED]
Zend - The PHP Company   | http://www.zend.com/

Reply via email to