在 Zend_Form 中,如何避免 Zend_Validate_Email 产生多个错误? [英] In a Zend_Form, how to avoid Zend_Validate_Email from generating multiple errors?

查看:17
本文介绍了在 Zend_Form 中,如何避免 Zend_Validate_Email 产生多个错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆