2016-05-17 74 views
0

我遇到Validating Top Level Domains問題。基本上,任何.tech作爲頂級域名的郵件驗證失敗。使用Zend驗證頂級域名1

我已經繼承了這個項目,並且不太瞭解Zend,但我已經將問題追溯到主機名無效here is the code on GitHub;

// Match hostname part 
    if ($this->_options['domain']) { 
     $hostname = $this->_validateHostnamePart(); 
    } 

    $local = $this->_validateLocalPart(); 

    // If both parts valid, return true 
    if ($local && $length) { 
     if (($this->_options['domain'] && $hostname) || !$this->_options['domain']) { 
      return true; 
     } 
    } 

    return false; 

現在,我在這裏有一些本地代碼;

class Form_InviteToSpaceForm extends Twitter_Bootstrap_Form_Horizontal 
{ 

    public function init() 
    { 

     // Set the method for the display form to POST 
     $this->setMethod('post'); 
     $this->setAction('/team'); 

     $this->addElement('textarea', 'email', array(
      'label'  => 'Email addresses', 
      'dimension' => 10, 
      'required' => true, 
      'placeholder' => "[email protected]                                                            [email protected]",//line breaks don't work on placeholders, have to force line wrap with spaces 
      'filters'  => array('StringTrim'), 
      'validators' => array(
       array('validator' => 'NotEmpty'), 
       array('validator' => 'EmailAddress', 'options' => array('messages' => array('emailAddressInvalidFormat' => 'Incorrect email address specified.'))) 
      ) 
     )); 

如果我註釋掉與上次array('messages' => array('emailAddressInvalidFormat' => 'Incorrect email address specified.')))那麼這整個驗證避免了線。但我不想避免使用這個。我只是希望能夠擴展並添加.tech或其他真正的客戶端。我如何與Zend做到這一點?

+0

「默認情況下,主機名會針對已知的頂級域名列表來檢查」 - 聽起來一個糟糕的設計 - 但你鏈接的文檔描述如何關閉它。 – symcbean

回答

0

您可以編寫自定義的驗證從Zend的驗證

class My_Validate_Hostname extends Zend_Validate_Hostname 
{ 
    public function __construct($options = array()) 
    { 
     parent::__construct($options); 
     $this->_validTlds = array_merge($this->_validTlds, array('tech')); 
    } 
} 

延伸,並通過它來電子郵件驗證

$emailValidator = new Zend_Validate_EmailAddress(array('messages' => array('emailAddressInvalidFormat' => 'Incorrect email address specified.'))); 
$emailValidator->setHostnameValidator(new My_Validate_Hostname()); 
.... 
$this->addElement('textarea', 'email', array(
      'label'  => 'Email addresses', 
      'dimension' => 10, 
      'required' => true, 
      'placeholder' => "[email protected]                                                            [email protected]",//line breaks don't work on placeholders, have to force line wrap with spaces 
      'filters'  => array('StringTrim'), 
      'validators' => array(
       array('validator' => 'NotEmpty'), 
      ) 
     ))->addValidator($emailValidator);