ZF2 form validation not working properly for email

前端 未结 2 1124
梦谈多话
梦谈多话 2021-01-23 00:04

i have a input box with type=\"email\" and validation it with zend validator

 email 
\'email\' => a         


        
相关标签:
2条回答
  • 2021-01-23 00:45

    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,
                ),
            ),
        ));
    
    0 讨论(0)
  • 2021-01-23 00:46

    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 => '',
                        )
                    ),
                ),
            ),
    
    0 讨论(0)
提交回复
热议问题