-- Jacky Chen <[EMAIL PROTECTED]> wrote
(on Sunday, 13 July 2008, 03:56 PM +0800):
> how to set error message for required form validator? there is a
> username element that required is true, then three validators are added to
> it.The first is notEmpty validator with message "now allow empty",the second
> is
> stringLength validator with options of array(1,20,'messages'=>'username must
> between 1 and 20 characters long'),the third is a custom validator. all these
> validator are set breakChainOnFailure. Then i post the form with the username
> empty,and the notEmpty error message not display,but stringLength error
> messages.If i delete the notEmpty validator,then post the form with username
> empty,the default error message for the notEmpty is display.
>
> the code as following:
>
> <?php
> // ...
> // if post the form with receiver empty,the stringLength validator message
> display instead of notEmpty message
> $receiver->setRequired(true);
> $receiver->addValidator('notEmpty', true, array('messages'=>'not allow
> empty')); // if i delete this validator,then post the form with receiver
> empty,the default message for the notEmpty validator is display
This is because when the element is required, if no NotEmpty validator
is present, it adds one.
You're adding the messages incorrectly. The value of the messages key
should be an array of key/value pairs, with the keys corresponding to
the appropriate constant values for the validation errors. In this case,
setup the element as follows:
$receiver->addValidator('notEmpty', true, array('messages' => array(
'isEmpty' => 'not allow empty',
)));
For the stringlength validator below, you should define the message keys
'stringLengthTooShort' and 'stringLengthTooLong'.
> $receiver->addValidator('stringLength', true, array(1, 20, 'messages'=>
> 'username must between 1 and 20 characters long'));
> $receiver->addValidator('checkReceiver', true, array($this->db, 'messages'
> => 'username exists));
> // ...
--
Matthew Weier O'Phinney
Software Architect | [EMAIL PROTECTED]
Zend Framework | http://framework.zend.com/