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

时间:2013-07-18 13:11:52

标签: php zend-framework2

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

'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#$%&安培;!'? + / = ^ _`{|}〜 - ] + @ [A-ZA-Z0-9-] +(?:[A-ZA-Z0-9 - ] +) $ /'

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

我对这里善良的人有两个问题:

如何自定义该错误消息?我似乎无法找到正确的密钥,因为我很容易找到'emailAddressInvalidFormat'

另外,是否可以将所有错误归为一类?我的意思是。而不是张贴:

  

“您的电子邮件模式刚刚离开了建筑物,而您的电子邮件却无法发送   空白&您的电子邮件似乎无效“

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

  

“嘿蓓蕾,查看你的电子邮件,有些事情不对!”

感谢您一如既往的帮助。

更新

在这里投票给这个错误 https://github.com/zendframework/zend-validator/issues/41

4 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:2)

根据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 ,我还有 NoObjectExists 以确保电子邮件不在我的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',
    ),
),

答案 2 :(得分:0)

“输入与模式不匹配”​​消息实际上似乎是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.",
        )
    ),
),

答案 3 :(得分:0)

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

    $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
    );