ZF2 form validation not working properly for email

让人想犯罪 __ 提交于 2019-12-02 01:43:16

You don't need to use notEmpty, you only need to set the field as "required" and specify the error message:

    $this->add(array(
        'name' => 'email',
        'required' => true,
        'error_message' => 'Please entry e-mail.',
        'validators' => array(
            array(
                'name' => 'EmailAddress',
                'options' => array (
                    'messages' => array(EmailAddress::INVALID => 'Please specify a valid e-mail.'),
                ),
                'break_chain_on_failure' => true,
            ),
        ),
    ));

I have also the same dilemma where this "input does not match to expression" always appears instead of the message for EmailAddress::INVALID_FORMAT. But I found that the code you posted fixed the same error I had. This is my code.

'validators' => array(
            array (
                'name' => 'Regex',
                'options' => array(
                    'pattern'=>'/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/',
                    'messages' => array(
                        Regex::NOT_MATCH    => 'Please provide a valid email address.',
                    ),
                ),
                'break_chain_on_failure' => true
            ),
            array(
                'name' => 'EmailAddress',
                'options' => array(
                    'messages' => array(
                        EmailAddress::INVALID_FORMAT   => 'Please provide a valid email address.',
                        EmailAddress::DOT_ATOM         => '',
                        EmailAddress::INVALID_FORMAT   => '',
                        EmailAddress::INVALID_LOCAL_PART => '',
                        EmailAddress::QUOTED_STRING => '',
                    )
                ),
            ),
        ),
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!