2010-07-24 87 views
1

這很奇怪。我試圖在Zend表單中爲我的電子郵件驗證元素設置錯誤消息。其他字段正常工作 - 但不是電子郵件 - 什麼事?無法設置郵件驗證的錯誤消息-Zend-

碼位:

$name = new Zend_Form_Element_Text('name'); 
     $name ->setLabel('Name:') 
       ->setRequired(true) 
       ->addValidator('Alnum') 
       ->addValidator('NotEmpty'); 
     $name ->getValidator('NotEmpty')->setMessage('Please enter your name.'); 
     $name ->getValidator('Alnum')->setMessage('Name can only contain letters and spaces.'); 

$email = new Zend_Form_Element_Text('email'); 
     $email ->setLabel('Email:') 
      ->setRequired(true) 
       ->addFilter('StringToLower') 
       ->addValidator('NotEmpty') 
       ->addValidator('EmailAddress'); 

     $email ->getValidator('NotEmpty')->setMessage('Please enter your email address.'); 
     $email ->getValidator('EmailAddress')->setMessage('Email is not in proper format.'); 

名稱的作品,但電子郵件不能正常工作。

我仍然得到defauly錯誤消息:'' is no valid email address in the basic format [email protected]

回答

6

你需要指定的郵件類型:

$email->getValidator('emailAddress')->setMessage("'%value%' is not valid, use something like [email protected]",Zend_Validate_EmailAddress::INVALID_FORMAT); 

以此爲例子:

protected $_messageTemplates = array(
     self::INVALID   => "Invalid type given, value should be a string", 
     self::INVALID_FORMAT  => "'%value%' is no valid email address in the basic format [email protected]", 
     self::INVALID_HOSTNAME => "'%hostname%' is no valid hostname for email address '%value%'", 
     self::INVALID_MX_RECORD => "'%hostname%' does not appear to have a valid MX record for the email address '%value%'", 
     self::INVALID_SEGMENT => "'%hostname%' is not in a routable network segment. The email address '%value%' should not be resolved from public network.", 
     self::DOT_ATOM   => "'%localPart%' can not be matched against dot-atom format", 
     self::QUOTED_STRING  => "'%localPart%' can not be matched against quoted-string format", 
     self::INVALID_LOCAL_PART => "'%localPart%' is no valid local part for email address '%value%'", 
     self::LENGTH_EXCEEDED => "'%value%' exceeds the allowed length", 
    ); 
+0

是否有一種簡單的方法可以生成一條適用於所有這些錯誤的錯誤消息? IE:「您的電子郵件地址有問題。」 謝謝! – Joel 2010-07-29 02:43:18

+0

我想你可以擴展emailAddress驗證器並更改受保護的變量$ _messageTemplates,這樣你甚至不需要設置消息。 http://stackoverflow.com/questions/1811085/zend-framework-custom-validation-classes http://framework.zend.com/manual/en/zend.validate.writing_validators.html – 2010-07-29 14:49:12

0

您需要創建自定義錯誤類。在我的情況:

我在圖書館/ Zend公司/驗證文件夾

class Zend_Validate_Email extends Zend_Validate_Abstract 
{ 

    const INVALID = 'Email is required'; 
    protected $_messageTemplates = array(
    self::INVALID => "Invalid Email Address", 
    ); 

    public function isValid($value) 
    { 
     if(filter_var($value, FILTER_VALIDATE_EMAIL) === false) { 
      $this->_error(self::INVALID); 
      return false; 
     } else { 
      // We only check the presence of a dot on the domain part 
      $components = explode("@", $value); 
      $domain = $components[1]; 

      if (strpos($domain, ".") === false) { 
       $this->_error(self::INVALID); 
       return false; 
      } 

      return true; 
     } 
    } 
} 

創建自定義的電子郵件驗證類,在我的形式作爲

$email_validate = new Zend_Validate_Email(); 

$neValidator = new Zend_Validate_NotEmpty(); 

$neValidator->setMessage('Please enter email.'); 

$emailaddress = new Zend_Form_Element_Text('EmailAddress'); 

$emailaddress->setRequired(true) 

      ->setAttrib('size', '30') 

      ->addFilter('StripTags') 

      ->setAttrib('MaxLength',100) 

      ->addValidator($neValidator,TRUE) 

       ->addValidator($email_validate,TRUE) 

      ->setAttrib('class','textbox') 

      ->setDecorators($decorators); 

希望它會幫助你

0
$email_validate = new Zend_Validate_Email(); 
$neValidator = new Zend_Validate_NotEmpty(); 
$neValidator->setMessage('Please enter email.'); 
$emailaddress = new Zend_Form_Element_Text('EmailAddress'); 
$emailaddress->setRequired(true)