在Zend_Form中,如何避免Zend_Validate_Email产生多个错误? [英] In a Zend_Form, how to avoid Zend_Validate_Email from generating multiple errors?
问题描述
我正在构建一个ZendFramework应用程序,该应用程序作为登录表单来询问电子邮件地址和密码-在尝试通过登录尝试访问数据库之前,先验证电子邮件地址似乎很有意义,因为无效的电子邮件永远不会导致有效命中. Zend_Validate_EmailAddress似乎是正确的方法,但是我遇到了一个问题,即它会产生多个错误(在代码后的底部,是问题).
I am building a ZendFramework application which as a login form asking for an email address and password - it seemed to make sense to validate the email address before hitting the database with the login attempt, as an invalid email would never lead to a valid hit. Zend_Validate_EmailAddress seemed like the right way to go, but I am having an issue with it generating multiple errors (question at the bottom, after the code).
我的表单当前具有以下内容
My form currently has the following
//WPMail_Form_Login::init()
$email = $this->addElement('text', 'email', array(
'label'=>'Email',
'required'=>true,
'filters'=>array('stringtrim'),
'validators'=>array(array('emailaddress', true, array(
'messages'=>array(
'emailAddressInvalidHostname'=>'Your email address is invalid',
'emailAddressInvalidFormat'=>'Your email address is invalid',
'...'=>'(repeat for all message templates)'
)
))),
));
在控制器中,我将表单直接传递给视图:
In the controller I directly pass the form into the view:
// WPMail_AuthController::loginAction()
$this->view->form = $form;
在视图中,它是直接回显的:
And in the view, it's directly echo'd:
// views/scripts/auth/login.phtml
<?php echo $this->form ?>
目前的结果是这样的:
- Your email address is invalid
- 'asda!!!' does not match the expected structure for a DNS hostname
- 'asda!!!' does not appear to be a valid local network name
我想知道的是:是否可以以仅产生单个电子邮件无效错误的方式配置Zend _
Validate _
EmailAddress?我的意思是配置",而不是扩展类并用我自己的逻辑覆盖.
What I want want to know is: Is it possible to configure Zend_
Validate_
EmailAddress in such a way that it only produces a single email-invalid error? By 'configure' I mean, without extending the class and overriding the logic with my own.
TIA.
推荐答案
Zend Form Element具有多种可用于自定义消息的方法.从文档中还不清楚,但是addErrorMessage()会在验证失败时设置一条自定义错误消息.
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);
请参见 http://framework.zend.com/manual/en/zend.form.elements.html#zend.form.elements.validators.errors
这篇关于在Zend_Form中,如何避免Zend_Validate_Email产生多个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!