« Return to Thread: Zend_Form

Re: Zend_Form

by Simon Corless :: Rate this Message:

Reply to Author | View in Thread

Ok so how would i set custom error messages making use of:

$form->addElement('text', 'name', array('required' => true, 'filters' => array('StringTrim', 'StringToLower'), 'label' => 'Name', 'description' => 'A unique name for the action, letters only.'));

As whatever I do I cannot get it to work, from reading the mailing list I assume that it cannot be done in this way?

Therefore I would need to create each element then add each element to the form?

I'm just creating my own selection of elements and hopefully more of it will become clear, you're right it would be easier to use the translation components, however is this not extra burden for my site, when ultimatley it's just in English? (Short sighted maybe, but from an efficiency point of view how does using translation stand as opposed to not using it?).

I'm sure it will come together, it's the transition from using code I've written and know inside out, to using someone elses code and hooking bits on here and there.

Simon

Matthew Weier O'Phinney-3 wrote:
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. :-)

 « Return to Thread: Zend_Form