Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自定义Zend Framework 2电子邮件字段验证程序错误消息_Php_Zend Framework2 - Fatal编程技术网

Php 自定义Zend Framework 2电子邮件字段验证程序错误消息

Php 自定义Zend Framework 2电子邮件字段验证程序错误消息,php,zend-framework2,Php,Zend Framework2,我有一个输入过滤器,其电子邮件字段的验证程序配置如下所示 'validators' => array( array ( 'name' => 'EmailAddress', 'options' => array( 'messages' => array( 'emailAddressInvalidFormat' => "Email address doesn't appear

我有一个输入过滤器,其电子邮件字段的验证程序配置如下所示

'validators' => array(
    array (
        'name' => 'EmailAddress',
        'options' => array(
            'messages' => array(
                'emailAddressInvalidFormat' => "Email address doesn't appear to be valid.",
            )
        ),
    ),
    array (
        'name' => 'NotEmpty',
            'options' => array(
                'messages' => array(
                    'isEmpty' => 'Email address is required',
                )
            ),
        ),
    ),
),
这是可行的,这一部分很好,但我会永远被这里的业务部门嘲笑的是,如果我发布一个向用户吐出此错误消息的应用程序:

输入与模式不匹配

“/^[a-zA-Z0-9.!$%&”+/=?^ `{124;}-]+@[a-zA-Z0-9-]+(?:[a-zA-Z0-9-]+)$/”

有一个奇怪的书呆子喜剧埋在那里(是的,我知道它是准确的,但,罗夫)

我有两个问题要问善良的灵魂:

如何自定义该错误消息?我似乎找不到正确的键,因为我很容易找到
“emailAddressInvalidFormat”

还有,是否可以将所有错误汇总为一个?我的意思是。而不是张贴:

“您的电子邮件模式刚刚离开大楼&您的电子邮件无法发送 空白&您的电子邮件似乎无效”

我可以在电子邮件中发送“单一故障”消息吗

“嘿,巴德,检查你的电子邮件,有点不对劲!”

谢谢你一如既往的帮助

更新

在这里投票支持这个错误

“输入与模式不匹配”消息似乎是
Zend\Validator\Regex::not_match
实际上,不是属于
Zend\Validator\EmailAddress
类的

从您的代码中,不清楚您在何处以及是否使用了
Regex
验证器。我相信,
EmailAddress
不会在内部使用
Regex

如果要自定义
Regex
消息,它可能如下所示:

array (
    'name' => 'Regex',
    'options' => array(
        'messages' => array(
            'regexNotMatch' => "Not so nerdy message here.",
        )
    ),
),

在ZF2中尝试使用此自定义邮件进行电子邮件验证:

 'validators' => array(
                array( 
                    'name' => 'EmailAddress',
                    'options' => array( 
                        'messages' => array(
                        \Zend\Validator\EmailAddress::INVALID_FORMAT => '"Hey bud, check your email, something ain\'t right!"' 
                        )             
                    )                   
                )             
            )         

不可能只指定一条消息,但它也应该起作用:

    $message = sprintf(
        $this->getTranslate()->translate(
            '%s filled is not a valid e-mail address, please inform a valid in the field.'
        ),
        $entity
    );

    return array(
        'name' => 'EmailAddress',
        'options' => array(
            'messages' => array(
                \Zend\Validator\EmailAddress::INVALID            => $message,
                \Zend\Validator\EmailAddress::INVALID_FORMAT     => $message,
                \Zend\Validator\EmailAddress::INVALID_HOSTNAME   => $message,
                \Zend\Validator\EmailAddress::INVALID_MX_RECORD  => $message,
                \Zend\Validator\EmailAddress::INVALID_SEGMENT    => $message,
                \Zend\Validator\EmailAddress::DOT_ATOM           => $message,
                \Zend\Validator\EmailAddress::QUOTED_STRING      => $message,
                \Zend\Validator\EmailAddress::INVALID_LOCAL_PART => $message,
                \Zend\Validator\EmailAddress::LENGTH_EXCEEDED    => $message,
            ),
        ),
        'break_chain_on_failure' => true
    );

根据ZF2,电子邮件地址验证包括:

  $this->add(array(
        'name'       => 'email',
        'required'   => true,
        'validators' => array(
            array(
                'name' => 'EmailAddress',
                'options' => array(
                    'message' => array(
                        \Zend\Validator\EmailAddress::INVALID =>   "Invalid type given. String expected",
                        \Zend\Validator\EmailAddress::INVALID_FORMAT     =>"The input is not a valid email address. Use the basic format local-part@hostname",
                        \Zend\Validator\EmailAddress::INVALID_HOSTNAME   =>"'%hostname%' is not a valid hostname for the email address",
                        \Zend\Validator\EmailAddress::INVALID_MX_RECORD  =>"'%hostname%' does not appear to have any valid MX or A records for the email address" ,
                        \Zend\Validator\EmailAddress::INVALID_SEGMENT    =>"'%hostname%' is not in a routable network segment. The email address should not be resolved from public network",
                        \Zend\Validator\EmailAddress::DOT_ATOM           =>"'%localPart%' can not be matched against dot-atom format" ,
                        \Zend\Validator\EmailAddress::QUOTED_STRING      =>"'%localPart%' can not be matched against quoted-string format",
                        \Zend\Validator\EmailAddress::INVALID_LOCAL_PART =>"'%localPart%' is not a valid local part for the email address" ,
                        \Zend\Validator\EmailAddress::LENGTH_EXCEEDED    =>"The input exceeds the allowed length",
                        ),
                ),
            ),
        ),
    ));
如上文所述,额外的验证可以是Regex,如果电子邮件地址用于登录,我也没有对象存在以确保电子邮件不在my
db
中:

array(
    'name'      => 'DoctrineModule\Validator\NoObjectExists',
    'options' => array(
        'object_repository' => $sm->get('doctrine.entitymanager.orm_default')->getRepository('MyMudole\Entity\User'),
        'fields'            => 'email',
        'message'=>'This email is associated with another user! Please use another email',
    ),
),

要避免出现错误时出现多条消息,请添加验证器
'break\u chain\u on\u failure'=>true,
。我可以将其与单个错误消息一起使用吗?如何指定?在我看来,这似乎是EmailAddress验证程序中的一个错误。我通常只设置一条消息,当验证器返回false时使用:'validators'=>array(array('name'=>'EmailAddress','options'=>array('message'=>'Invalid e-mail address',),(注意“message”的单数形式。这在EmailAddress验证程序中除外,EmailAddress验证程序仍然会输出模式匹配消息,而模式匹配消息实际上来自Regex验证程序(不匹配)。第2部分:我浏览了代码,当splitEmailParts返回false时,似乎会发生这种情况。但看起来它确实在该点设置了正确的错误,然后才返回。不确定Regex消息来自何处。因此,如果使用仅包含EmailAddress的文本元素,则Email元素会注册一个额外的RegexValidator验证器没有连接正则表达式验证器?我想说您在正则表达式验证器失败时设置了
break\u chain\u
(),并在正则表达式验证器内部设置了错误消息。不幸的是,这不起作用,我收到的是“没有针对“regexNotMatch”的消息模板”。ZF 2.2.6