在 Zend_Form 中,如何避免 Zend_Validate_Email 生成多个错误?

发布于 2024-07-30 12:38:28 字数 1312 浏览 4 评论 0原文

我正在构建一个 ZendFramework 应用程序,它作为要求电子邮件地址和密码的登录表单 - 在尝试登录尝试访问数据库之前验证电子邮件地址似乎是有意义的,因为无效的电子邮件永远不会导致有效的命中。 Zend_Validate_EmailAddress 似乎是正确的方法,但我遇到了它生成多个错误的问题(问题位于底部,代码之后)。

我的表单目前有以下内容

//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)'
        )
    ))),
));

在控制器中我直接将表单传递到视图中:

// WPMail_AuthController::loginAction()
$this->view->form = $form;

在视图中,它直接 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_验证_EmailAddress 的方式是否只产生一个电子邮件无效错误? 我所说的“配置”是指,无需扩展类并用我自己的逻辑覆盖逻辑。

TIA。

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 ?>

The result is currently something like this:

- 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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

决绝 2024-08-06 12:38:29

Zend Form Element 有多种可用于自定义消息的方法。 从文档中还不是很清楚,但 addErrorMessage() 在验证失败时设置一条自定义错误消息。

因此,您的示例如下所示:

$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 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

你怎么这么可爱啊 2024-08-06 12:38:29

由于这些消息是由 one 验证器生成的,我认为不可能 :-(

Zend_Validate_EmailAddress::isValid 方法执行所有验证, on 生成整个错误,似乎

一个“hacky”解决方案是在控制器中迭代错误,并删除除第一个错误之外的所有字段,对于每个具有多个错误的字段...但是我真的不喜欢那个声音......

当然,您可以继承并修改默认行为...但您表示您不想这样做,所以...

不过,如果我'我错了,有一种方法,我很好奇;-)

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 ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文