zend 表单电子邮件验证
我有以下代码来生成用户电子邮件地址的输入字段,
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email:')
->addFilters(array('StringTrim', 'StripTags'))
->addValidator('EmailAddress')
->addValidator(new Zend_Validate_Db_NoRecordExists(
array(
'adapter'=>Zend_Registry::get('user_db'),
'field'=>'email',
'table'=>'tbl_user'
)))
->setRequired(true)
->setDecorators(array(
array('Label', array('escape'=>false, 'placement'=>'append')),
array('ViewHelper'),
array('Errors'),
array('Description',array('escape'=>false,'tag'=>'div')),
array('HtmlTag', array('tag' => 'div')),
));
$this->addElement($email);
现在的问题是如果用户输入无效的电子邮件主机名,则会生成 3 个错误。假设用户输入“admin@l”作为电子邮件地址,错误将是
*“l”不是电子邮件地址“admin@l”的有效主机名
*“l”与 DNS 主机名的预期结构不匹配
*“l”似乎是本地网络名称,但不允许本地网络名称,
我只想它只给出一个自定义错误,而不是所有这些错误。如果我通过 addErrorMessage 方法设置错误消息“无效的电子邮件地址”,它将再次针对 db_validation 生成相同的消息。
I have the following code to generate an input field for user's email address
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email:')
->addFilters(array('StringTrim', 'StripTags'))
->addValidator('EmailAddress')
->addValidator(new Zend_Validate_Db_NoRecordExists(
array(
'adapter'=>Zend_Registry::get('user_db'),
'field'=>'email',
'table'=>'tbl_user'
)))
->setRequired(true)
->setDecorators(array(
array('Label', array('escape'=>false, 'placement'=>'append')),
array('ViewHelper'),
array('Errors'),
array('Description',array('escape'=>false,'tag'=>'div')),
array('HtmlTag', array('tag' => 'div')),
));
$this->addElement($email);
now the problem is if user enter invalid hostname for email, it generate 3 errors. lets say user enter 'admin@l' as email address, and the errors will be
* 'l' is no valid hostname for email address 'admin@l'
* 'l' does not match the expected structure for a DNS hostname
* 'l' appears to be a local network name but local network names are not allowed
I just want it to give only one custom error instead of all these. If I set error message "Invalid Email Address" by addErrorMessage method, it will again generate the same message against the db_validation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
嗯,这是一个迟到的答案,但我认为总是有用的。
只需添加 true 作为
Zend 文档中 addValidator() 的第二个参数 (http://framework.zend.com/apidoc/1.8/):
这里签名:
所以代码是:
Well, it's a late answer but I think is always useful.
Simply add true as second param of addValidator()
From Zend docs (http://framework.zend.com/apidoc/1.8/):
Here the signature:
So the code is:
您必须创建 Zend_Validate_EmailAddress 类的实例并调用 setMessages 方法,然后重写您喜欢的消息,以删除您提到的消息,它会是这样的:
我希望这可以帮助某人:-)
You have to create an instance of the Zend_Validate_EmailAddress class and call the setMessages method and then override the messages that you like, to remove the ones that you mention it would be something like this:
I hope this help somebody :-)
您可以使用自定义验证器。在项目根目录的 library 文件夹
和 form.php
Validate 文件夹中创建一个文件 Email.php不知道它是否有效,但对我来说,它在电话情况下有效 由 http://softwareobjects.net/technology/ 提供其他/zend-framework-1-10-7-电话-验证器/
you can use custom validator. create a file Email.php inside folder Validate in your library folder at the root of project
and in you form.php file
Don't know if it works or not but for me it worked in case of telephone. Courtesy of http://softwareobjects.net/technology/other/zend-framework-1-10-7-telephone-validator/
它似乎缺少很多行......
可能应该使用这个:
$mailValidator = new Zend_Validate_EmailAddress();
您还可以执行一些其他验证,请参见此处: http://framework.zend .com/manual/en/zend.validate.set.html
It seems to be missing quite a few lines...
probably should use this:
$mailValidator = new Zend_Validate_EmailAddress();
you can also do some other validations see here: http://framework.zend.com/manual/en/zend.validate.set.html
使用自定义验证器是我发现避免此问题的唯一方法。
如果您想要的是:
那么我建议您这样做像这样的东西:
$this->getAttrib('user_id')
代表当前用户的 ID。这里有三个验证器,它们的第二个参数
$breakOnFailure
都设置为false
,所以如果一个验证器失败,其他验证器将不会被调用。第一个验证器是email,这是我自己的自定义验证器:
您可以在您的库中添加此验证器,例如在 /application/library/My/Validate 中,然后添加
$this->addElementPrefixPath('My_Validate', 'My/Validate', 'validator');
进入你的表格。当然,您需要将“My”替换为您的图书馆名称。
现在,如果电子邮件格式错误,它将始终显示“您的电子邮件地址无效。”。如果您的电子邮件太长并且不适合您的数据库字段(例如 VARCHAR(100)),它将显示您的 stringLength 验证器错误,在最后一种情况下,如果数据库中已存在条目,则仅此将显示错误。
当然,您可以在自定义验证器中添加更多方法并重载 setMessages,以便您可以显示自己的消息,无论您正在处理什么表单。
希望它可以帮助别人!
Using a custom validator is the only way I found to avoid this problem.
If what you want is:
Then I suggest you to do something like this:
$this->getAttrib('user_id')
represents the current user's id.There are three validators here, all of them have their second parameter
$breakOnFailure
set tofalse
, so if a validator fails, the other ones won't be called.The first validator is email, which is my own custom validator:
You can add this validator in your library, in /application/library/My/Validate for example, and then add
$this->addElementPrefixPath('My_Validate', 'My/Validate', 'validator');
into your form. Of course, you need to replace "My" by the name of your library.
Now if an email is in the wrong format, it will always display 'Your email address is not valid.'. If your email is too long and doesn't fit into your database field (VARCHAR(100) for example), it's going to show your stringLength validator errors, and in the last case, if an entry already exists in the database, only this error will be shown.
Of course you can add more methods into your custom validator and overload setMessages, so that you can display your own messages whatever the form you are working on.
Hope it can help someone!