2012-06-16 146 views
4

我已經閱讀了一些關於此的帖子,但我無法解決我的問題。 當我嘗試驗證我的Zend表單驗證碼時,即使使用正確的文本,它也總是失敗。 這裏是我的代碼:Zend_Form驗證碼不驗證

//這裏我把我的形式

public function contactAction() 
{ 
    $this->view->form = new Forms_ContactForm(); 
} 

//我的形式

class Forms_ContactForm extends Twitter_Form 
{ 
public function init() 
{ 
    $this->setAction('email/email/contact'); 
    $this->setMethod('post'); 

    $this->addElement('text', 'strFirstName', array( 
      'required' => true, 
      'label' => 'Your First Name')); 

    $this->addElement('text', 'strLastName', array( 
      'required' => true, 
      'label' => 'your Last Name')); 

    $this->addElement('text', 'strEmail', array('validators' => array(
      array('EmailAddress')), 
      'required' => true, 
      'label' => 'Your Email')); 

    $this->addElement('textarea', 'strContent', array( 
      'required' => true, 
      'label' => 'Your message')); 

    $this->addElement('captcha', 'captcha', array(
      'label'  => 'Please enter the 5 letters displayed below:', 
      'required' => true, 
          'name'  => 'captchaField', 
      'captcha' => 'image', 
      'captchaOptions' => array( 
          'captcha' => 'image', 
          'font'=> 'static/font/arial.ttf', 
          'imgDir'=>'static/img/captcha', 
          'imgUrl'=> 'static/img/captcha/', 
        'wordLen' => 5, 
        'fsize'=>20, 
        'height'=>60, 
        'width'=>200, 
        'gcFreq'=>50, 
        'expiration' => 300) 

      )); 


    $this->addElement('submit', 'submit', array('class' => 'Submit')); 
} 
} 

//我的行動來發送

public function contactAction() 
{ 
    if($this->_request->isPost()) 
    { 
     $objForm = new Forms_ContactForm(); 

     if($objForm->isValid($_POST)) 
     { 
      $arrParams = $this->_request->getParams(); 
      if($arrParams['strFirstName'] && $arrParams['strLastName'] && $arrParams['strEmail'] && $arrParams['strContent']) 
      { 
       $this->_objEmail = new Zend_Mail(); 
       $this->_objEmail ->setFrom($arrParams['strEmail']); 
       $this->_objEmail ->setSubject('C'); 
       $this->_objEmail ->setBodyHtml('Message from: '. $arrParams['strFirstName'] . ' ' . $arrParams['strLastName'] . 
               '<BR>eMail address: ' . $arrParams['strEmail'] . '<BR><BR>' 
               . $arrParams['strContent']); 

       $this->_objEmail ->addTo('[email protected]'); 

       $this->view->bolSent = $this->_objEmail->send(); 
      } 
     } 
     else 
      $this->view->form = $objForm; 
    } 
} 

看起來,在我的contactAction中,它生成一個新的驗證碼,所以這就是爲什麼它與我所屬的那個匹配但是我不知道如何解決這個問題。

謝謝你的時間和幫助!

剛纔看到的東西不可靠:當我接觸動作傾倒我的$ _ POST這裏是我的結果:

array 
'strFirstName' => string 'fghjfghj' (length=8) 
'strLastName' => string 'ffffffff' (length=8) 
'strEmail' => string '[email protected]' (length=14) 
'strContent' => string 'fewfew' (length=6) 
'captchaField' => string 'cebfe69ead38dba86a6b557dc8853b24' (length=32) 

我剛剛進入dosent甚至會出現,而是我有驗證碼凱驗證碼!! ??

再次編輯

感謝您的重播! 即使有你的改變,仍然不存在,但我認爲我得到了什麼錯誤。 這裏是我的驗證碼現場HTML:

<div class="control-group"> 
    <label class="control-label required" for="captchaField-input">Please enter the 5 letters displayed below:</label> 
    <div class="controls"> 
     <img width="200" height="60" src="static/img/captcha/ab2d15044a637338064b39cfd2675837.png" alt=""> 
     <input type="hidden" id="captchaField-id" value="ab2d15044a637338064b39cfd2675837" name="captchaField[id]"> 
     <input type="text" value="" id="captchaField-input" name="captchaField[input]"> 
     <input type="text" value="ab2d15044a637338064b39cfd2675837" id="captchaField" name="captchaField"> 
    </div> 
</div> 

當我看看我的PARAMS發送,這裏是我的了:

captchaField ab2d15044a637338064b39cfd2675837 captchaField [ID] ab2d15044a637338064b39cfd2675837 captchaField [輸入] 6af7u

似乎cptchaField overwright [ID]和[輸入]

我覺得我需要刪除該captchaField但不知道到目前爲止!

我可以做到這一點與JS,但必須有一個乾淨的方式來這樣做!

再次編輯

進出口使用AJAX來提交表單,以連載。這可能是問題,不好看看。

編輯TER

它不是由AJAX造成的。 如果我手動刪除行:

<input type="text" value="ab2d15044a637338064b39cfd2675837" id="captchaField" name="captchaField"> 

與螢火蟲,一切正常,並驗證碼驗證很好。現在的問題是如何正確刪除此行......

SOLUTION

掙扎了很多之後,這裏是解決方案(刪除裝飾)!

$captcha = new Zend_Form_Element_Captcha('captchaField', 
     array('label' => "Please enter the 5 letters displayed below:", 
      'required'=>true, 
      'captcha' => array(
      'captcha' => 'image', 
      'font'=> 'static/font/arial.ttf', 
      'imgDir'=>'static/img/captcha', 
      'imgUrl'=> 'static/img/captcha/', 
      'wordLen' => 5, 
      'fsize'=>20, 
      'height'=>60, 
      'width'=>200, 
      'gcFreq'=>50, 
      'expiration' => 300 
     ) 
    )); 

    $this->addElement($captcha);  
    $this->getElement('captchaField')->removeDecorator("viewhelper"); 
+0

如何在您的驗證碼元素顯示在您的視圖文件? – Haroon

+0

是視圖文件中的代碼還是它是頁面的HTML源代碼? – Haroon

+0

我的查看代碼只是echo $ this-> form 這是源代碼,由zend自動構建, – mokk

回答

4

掙扎後很多,這裏是解決方案(我只需要刪除裝飾器)!

$captcha = new Zend_Form_Element_Captcha('captchaField', 
    array('label' => "Please enter the 5 letters displayed below:", 
     'required'=>true, 
     'captcha' => array(
     'captcha' => 'image', 
     'font'=> 'static/font/arial.ttf', 
     'imgDir'=>'static/img/captcha', 
     'imgUrl'=> 'static/img/captcha/', 
     'wordLen' => 5, 
     'fsize'=>20, 
     'height'=>60, 
     'width'=>200, 
     'gcFreq'=>50, 
     'expiration' => 300 
    ) 
)); 

$this->addElement($captcha);  
$this->getElement('captchaField')->removeDecorator("viewhelper"); 

哈龍感謝你的幫助,你的時間:)

1

我想你的代碼需要像下面,我想在contactAction.php文件中public function contactAction()應該處理顯示窗體,當窗體已經公佈在行動):

//Display the form

$objForm = new Forms_ContactForm(); 

$this->view->form = $objForm; 



//Handle the form, when it has been posted including validation etc 

if($this->_request->isPost()) 
{ 
    if($objForm->isValid($_POST)) 
    { 
     //processing logic etc 
    }   
} 

當前您的代碼正在生成一個新的驗證碼,以確認數據輸入,因爲您在表單發佈後將實例化表單。這個表單的實例需要在表單發佈之前完成,正如我在上面的代碼示例中所展示的那樣。

編輯

試試這個:

$captcha = new Zend_Form_Element_Captcha('captchaField', 
    array('label' => "Please enter the 5 letters displayed below:", 
     'required'=>true, 
     'captcha' => array(
     'captcha' => 'image', 
     'font'=> 'static/font/arial.ttf', 
     'imgDir'=>'static/img/captcha', 
     'imgUrl'=> 'static/img/captcha/', 
     'wordLen' => 5, 
     'fsize'=>20, 
     'height'=>60, 
     'width'=>200, 
     'gcFreq'=>50, 
     'expiration' => 300 
    ) 
)); 

$this->addElement($captcha); 

編輯後,這個新代碼的工作我使用Zend Framework版本1.11.1

的是什麼版本的Zend框架的是你唱,以及如何你是否將Captcha顯示在你的視圖文件中?

當VAR傾銷我們你的輸出,爲elment你應該會類似於下面,其中「5g4ef」事情是你輸入到驗證碼輸入元素的數據驗證碼:

["captchaField"]=> array(2) { ["id"]=> string(32) "88bb26d62e9fa19b67937c35be4a8cc7" ["input"]=> string(4) "5g4ef" }

+0

感謝您的快速回答。不幸的是,即使按照您所說的方式更改我的代碼,我仍然無法驗證它。 – mokk

+0

再次感謝您的回放: – mokk

+0

@mokk np - 請讓我知道這是否適合您。我們希望其他人能夠知道這個問題是否解決。 – Haroon