2013-07-18 81 views
6

我有一個輸入過濾器,其驗證器配置爲一個電子郵件字段看起來像;自定義Zend Framework 2電子郵件字段驗證器錯誤消息

'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'

另外,是否可以將所有錯誤合併爲一個?我的意思是。而不是發佈:

「您的電子郵件模式,就像離開大樓&您的郵箱不能 空白&您的電子郵件不會出現有效」

我可以把一個「單點故障」郵件的消息?

「嗨,請檢查您的電子郵件,有些事情是不正確的!」

感謝您一如既往的幫助。

UPDATE

投票這裏 https://github.com/zendframework/zend-validator/issues/41

+2

爲了避免在發生錯誤時出現多條消息,請添加驗證器''break_chain_on_failure'=> true,'。 – Tounu

+0

我可以使用它與一個單一的錯誤消息?如何指定? – Saeven

+0

這對我來說好像是EmailAddress驗證器中的一個錯誤。我通常只需設置一個消息時要使用的驗證器返回false: '驗證'=>數組( \t \t \t \t \t \t陣列( \t \t \t \t \t \t \t '名稱'=> 'EmailAddress的', \t \t \t \t \t \t \t '選項'=>數組( \t \t \t \t \t \t \t \t '消息'=> '無效電子郵件地址', \t \t \t \t \t \t \t) \t \t \t \t \t \t) \t \t \t \t \t),(注意 '消息' 的單數形式。這個工作除了使用EmailAddress驗證器,它仍然吐出模式匹配消息,它實際上來自正則表達式驗證器(NOT_MATCH)。 – Ruben

回答

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.", 
     ) 
    ), 
), 
+0

不幸的是,這不起作用,我收到''regexNotMatch'沒有消息模板。 ZF 2.2.6 – Saeven

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!"' 
         )    
        )     
       )    
      )   
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 
    ); 
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 [email protected]", 
         \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', 
    ), 
), 
相關問題