In a Zend_Form, how to avoid Zend_Validate_Email from generating multiple errors?

无人久伴 提交于 2019-11-30 13:07:00

Zend Form Element has various methods you can use to customise messages . It's not terribly clear from the docs but addErrorMessage() sets a single custom error message on failed validation.

Your example would therefore look like:

$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email')
      ->setRequired(true)
      ->addFilter('stringtrim')
      ->addValidator('emailAddress', true)
      ->addErrorMessage('Your email address is invalid');
$this->addElement($email);

See http://framework.zend.com/manual/en/zend.form.elements.html#zend.form.elements.validators.errors

Check out this tutorial by Pádraic Brady on using Zend_Form.

Specifically the section "Step 4: Handling Error Messages with a Custom Decorator."

As those messages are generated by one validator, I do not think it is possible :-(

The Zend_Validate_EmailAddress::isValid method does all the validations, on generates the errors as a whole, it seems.

One "hacky" solution would be to iterate, in your controller, on the errors, and remove all but the first one, for each field that has more than one... But I don't really like the sound of that...


You could, of course, inherit than and modify the default behaviour... But you stated you didn't want to do that, so...


Still, if I'm wrong, and there is a way, I'm very curious about it ;-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!