表单元素电子邮件验证允许无效的电子邮

时间:2017-03-21 07:50:08

标签: php zend-framework2 zend-framework3

今天我发现Zend\Form\Element\Email没有使用Zend\Validator\EmailAddress进行电子邮件验证,而是使用以下Regex Validator:

'/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/' 

正则表达式验证程序允许使用example@world-test-de等电子邮件地址。

在提交错误报告之前,我想检查一下:

a)为什么默认情况下不使用EmailAddress验证程序?
b)正则表达式模式错误吗?

如果使用该代码,上述电子邮件地址有效:

$this->add(array(
    'type' => 'Zend\Form\Element\Email',
    'name' => 'emails',
    'options' => array(
        'label' => 'Notification recipients',
        'description' => 'separate multiple email addresses with comma',
    ),
    'attributes' => array(
        'multiple' => true
    ),
));

只要我更改默认电子邮件验证程序,验证就会正常运行:

$this->add(array(
    'type' => 'Zend\Form\Element\Email',
    'name' => 'emails',
    'options' => array(
        'label' => 'Notification recipients',
        'description' => 'separate multiple email addresses with comma',
    ),
    'attributes' => array(
        'multiple' => true
    ),
));
**$this->get('emails')->setEmailValidator(new \Zend\Validator\EmailAddress());**

1 个答案:

答案 0 :(得分:2)

我认为这不是一个错误 Zend\Form\Element\Email是类型为email的HTML5输入,因此可以实现HTML5规范 - > https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address

如你所见:

  

以下JavaScript和Perl兼容的正则表达式是   执行上述定义。

     

/ ^ [A-ZA-Z0-9#$%&安培;!?“ + / = ^ _`{|}〜 - ] + @一个-ZA-Z0-9(?: .A-ZA-Z0-9?) $ /

这与Zend用于验证电子邮件的正则表达式完全相同

相关问题