6

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

'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

4 回答 4

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!"' 
                        )             
                    )                   
                )             
            )         
于 2014-05-07T12:54:48.193 回答
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',
    ),
),
于 2015-11-23T21:07:13.513 回答
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
    );
于 2015-01-06T22:53:32.060 回答
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.",
        )
    ),
),
于 2013-09-05T18:12:47.973 回答