开发者

How to show just one (e.g. the first) error message to a form field with Zend framework where validation has failed?

Is there any way to do that with a decora开发者_开发百科tor or do I need to dig deeper into ZF?


If you have multiple validators attached to one form element and want only the first error message to pop up, you can set breakChainOnFailure option to TRUE for each validator. In this case, if one of the validators fails, all the subsequent validators are skipped.

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


if $foo is in an instance of Zend_Form and there is an form element username inside this form then $foo->username->getMessages() ; will return array of errors messages to show only one you simply need to do array_pop($foo->username->getMessages()); . Basically zend decorators uses error view helper to display the messages . You need to extend the default decorator and remove the view helper simply by echo


There are many ways you can do this. My personal favorite is to extend the validator you want to use to only return one custom message. For the email validator this is especially helpful since it shows some crazy error messages that you may not want to show anyway. You can see this method here: http://clintberry.com/2010/07/zend-form-email-validator-customizing-error-messages/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜