« Return to Thread: Zend_Form

Re: Zend_Form

by weierophinney :: Rate this Message:

Reply to Author | View in Thread

-- SiCo007 <simon@...> wrote
(on Thursday, 27 March 2008, 12:50 PM -0700):
>
> Thanks, a further question, what is the preffered way of setting custom
> validator messages, I couldn't get any of the ways I tried (both setting the
> message by grabbing the validator, which seems like an extra step, trying to
> pass the messages in an array in addElement and finally trying to set the
> message for the alllow empty.
>
> Will there be a simple (preferably as part of addElement) or does it already
> exist and I just haven't found it?!

It's documented and straightforward: You pass an array of error
code/message pairs to the 'messages' key in the params for the
validator:

    $element = new Zend_Form_Element(array(
        'validators' => array(
            array('NotEmpty', false, array(
                'messages' => array('isEmpty' => 'This is a custom message')
            )),
        )
    );

The trick is remembering that 'messages' should be an *array*, and that
it should contain key/value pairs of validation class constants/mesages.

You can also simply use translation files, which are the easier method.
In that case, you provide translations for each validation error code
you want a custom message for, and attach the translator:

    $form->setTranslator($translate);

You can use a translation object even if you only have one language; the
nice part is it future-proofs your site for additional languages. :-)

--
Matthew Weier O'Phinney
PHP Developer            | matthew@...
Zend - The PHP Company   | http://www.zend.com/

 « Return to Thread: Zend_Form