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

2023-05-30php开发问题
6

本文介绍了在 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 产生多个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17