开发者

Messages not working on Zend_Form_Element_Text

In a form, I have the following element:

    $email = new Zend_Form_Element_Text('username');
            $email
                    ->setLabel($this->getView()->l('E-mail'))
                    ->setRequired(TRUE)
                    ->addValidator('EmailAddress')
           开发者_JS百科         ->addValidator('Db_NoRecordExists', true,
                            array(
                                'table' => 'pf_user',
                                'field' => 'email',
                                'messages' => array(
                                    'recordFound' => 'This username is already registered',
                                )
                    ))
                    ->setErrorMessages(array(
                        'emailAddressInvalidFormat' => 'You must enter a valid e-mail',
                        'isEmpty' => 'You must enter an e-mail',
                        'recordFound' => 'This e-mail has already registered in out database'
                    ));
$form->addElement($email)

the problem is that I always I get the same message "You must enter a valid e-mail" (the first one). Does anybody knows what is the mistake??


Actually, what you're doing is the following :

  1. You set the errors on the element
  2. Zend now thinks that the element did not validate correctly and that the first error is "You must enter a valid e-mail"
  3. When you display the form, since you set errors, Zend will find them and display the first one it finds. If you switch the order then you'll find that whichever error you put up top will be the error you get.

The more correct way is to set the custom messages in the validator. When the validators are called to validate the element, if the validation fails, the validator will call the setErrorMessages on the element to set the custom errors you specify. Use this type of code below to set your custom messages.

$element->addValidator( array( 'Db_NoRecordExists', true, array( 
'messages' = array(
Zend_Validate_Db_Abstract::ERROR_NO_RECORD_FOUND => 'Myy custom no error record',
Zend_Validate_Db_Abstract::ERROR_RECORD_FOUND    => 'My custom record error'
)
) ) );

You'll find that usually there are consts in each validator class that specify one type of error. In this case, the consts are in the parent class of the DB_NoRecordExists class but usually you'll find them directly in the class near the top.


Basically by passing 'true' as second parameter to addValidator() you are saying the validator to break the chain whenever validator fails . Since "" is not an valid email address hence the first email validator fails and breaks the chain

From Zend Doc http://framework.zend.com/manual/en/zend.validate.validator_chains.html

In some cases it makes sense to have a validator break the chain if its validation process fails. Zend_Validate supports such use cases with the second parameter to the addValidator() method. By setting $breakChainOnFailure to TRUE, the added validator will break the chain execution upon failure, which avoids running any other validations that are determined to be unnecessary or inappropriate for the situation. If the above example were written as follows, then the alphanumeric validation would not occur if the string length validation fails:

$validatorChain->addValidator(
                    new Zend_Validate_StringLength(array('min' => 6,
                                                         'max' => 12)),
                    true)
               ->addValidator(new Zend_Validate_Alnum());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜