簡體   English   中英

自定義 Zend Framework 2 電子郵件字段驗證器錯誤消息

[英]Customizing Zend Framework 2 Email Field Validator Error Messages

我有一個輸入過濾器,其電子郵件字段的驗證器配置如下所示;

'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

試試這個用於 ZF2 中電子郵件驗證的自定義消息:

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

根據 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",
                        ),
                ),
            ),
        ),
    ));

並且額外的驗證可以是如上所述的正則表達式,如果使用電子郵件地址登錄,我也有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',
    ),
),

“輸入與模式不匹配”​​消息實際上似乎是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.",
        )
    ),
),

不可能只指定一條消息,但它也應該有效:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM