2012-12-18 70 views
1

我有一個表格檢查2個表格內數據庫中是否存在電子郵件。zend表格檢查記錄在數據庫中不存在

我使用Zend_Validate_Db_NoRecordExists來驗證,但它只檢查第二個。

任何想法爲什麼它不工作?

class Application_Form_ReferUser extends Zend_Form 
{ 
public $email, $freeDownload, $buyNow; 

public function init() 
{ 
    $this->setName('referUser'); 

    $EmailExists = new Zend_Validate_Db_NoRecordExists(
     array(
      'table' => 'referrals', 
      'field' => 'email' 
     ) 
    ); 

    $EmailExists2 = new Zend_Validate_Db_NoRecordExists(
     array(
      'table' => 'users', 
      'field' => 'email' 
     ) 
    ); 

    $EmailExists->setMessage('This e-mail is already taken'); 
    $EmailExists2->setMessage('This e-mail is already taken'); 

    $this->email = $this->createElement('text', 'email') 
        ->setLabel('Email') 
        ->addValidator($EmailExists) 
        ->addValidator($EmailExists2) 
        ->addValidator('EmailAddress') 
        ->setRequired(true); 

    $this->freeDownload = $this->createElement('button', 'btn_free_download') 
          ->setLabel('Free Download') 
          ->setAttrib('type', 'submit'); 

    $this->buyNow = $this->createElement('button', 'btn_buy_now') 
          ->setLabel('Buy Now') 
          ->setAttrib('type', 'submit'); 

    $this->addElements(array($this->email, $this->freeDownload, $this->buyNow)); 

    $elementDecorators = array(
     'ViewHelper' 
    ); 
    $this->setElementDecorators($elementDecorators); 
} 
} 

回答

1

我不認爲你在一個元素上多次添加同一個驗證器。檢查類Zend_Form_ElementaddValidator()行1153.根據代碼$this->_validators[$name] = $validator;,當我們添加相同的驗證器多次重複前者將被覆蓋。

我認爲你必須創建一個自定義窗體驗證或元素單獨使用isValid()和驗證。

+0

是的,我認爲這是真的,那覆蓋。所以,也許我要驗證它在控制器中。謝謝。 –

1

使用此:

addValidators (array $ validators, $ files = null) 

ejem:

$this->addValidators(array($EmailExists,$EmailExists2,'EmailAddress')); 

我的工作我自己

+0

都能跟得上它仍然無法正常工作仍然在做同樣的事情:(感謝反正 –

+1

招呼正確的代碼是這樣的(不陣列)* $驗證=新Zend_Validate_Db_RecordExists(「用戶」,「EMAILADDRESS」); –

+0

謝謝,我會嘗試一下TMR,給你一個反饋 –

1
$this ->addElement($email = new Zend_Form_Element_Text('email')); 

$email->setLabel('E-mail') 
     ->addValidator('Db_NoRecordExists', false, array('table' => 'table1', 'field' => 'email')) 
     ->addValidator('Db_NoRecordExists', false, array('table' => 'table2', 'field' => 'email')) 
     ->addValidator('EmailAddress', false); 
1

嘗試像下面(未經測試的)一些事情,

$emailNotEmpty = new Zend_Validate_NotEmpty(); 
     $emailNotEmpty->setMessage('You must enter a email address.'); 
     $emailFormat = new Zend_Validate_Regex('/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/'); 
     $emailFormat->setMessage('You must enter a valid email address.'); 
     $emailUnique1 = new Zend_Validate_Db_NoRecordExists('referrals', 'email'); 
     $emailUnique1->setMessage('This email address is already registered.'); 
     $emailUnique2 = new Zend_Validate_Db_NoRecordExists('users', 'email'); 
     $emailUnique2->setMessage('This email address is already registered.'); 
     $email = new Zend_Form_Element_Text('email', array(
      'label' => 'Email Address:', 
      'class' => 'text-size text hastip', 
      'title' => $qtips_messages['key_email'], 
      'tabindex' => '2', 
      'required' => true, 
      'value'=> '', 
      'filters' => array('StringTrim'), 
      'validators' => array(
      array($emailNotEmpty, true), 
      array($emailFormat, true), 
      array($emailUnique1,true), 
      array($emailUnique2,true), 
     ), 
     'decorators' => $this->requiredElementDecorators, 
     'description' => '<img src="../../images/star.png" alt="required" />', 
     )); 
     $this->addElement($email); 
相關問題