« Return to Thread: Zend_Form

Re: Zend_Form

by weierophinney :: Rate this Message:

Reply to Author | View in Thread

-- SiCo007 <simon@...> wrote
(on Friday, 28 March 2008, 04:37 AM -0700):
>
> 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?

Well, no, it can't -- because you don't have any validators associated
with it. You *have* made it required, but because you don't explicitly
set a NotEmpty validator, there's no way to set the message.

Add a validator, and add messages, as I showed you in my previous
message; if you are only testing for NotEmpty, and want a custom
message, add the validator explicitly. Add your validators just like
you've added filters in your example:



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

You need to create elements somehow.

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

> 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 can't speak to the performance of the I18N components as I've done no
benchmarking with them, but they wouldn't likely add that much overhead
to your application.

> 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. :-)
> >
> >
>
>
> -----
> Simon Corless
>
> http://www.ajb007.co.uk/
> --
> View this message in context: http://www.nabble.com/Zend_Form-tp16324818p16349332.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>

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

 « Return to Thread: Zend_Form