2011-01-09 22 views
0

我有點奇怪的問題。我正在測試Zend並需要在表單中添加reCaptcha字段。我遵循Zend文檔中給出的例子,它沒有工作(習慣了)。正在獲取'錯誤驗證碼'錯誤。Zend - Recaptcha奇怪的問題 - 工作但並不如預期

在閱讀了一段時間後,我終於設法讓它工作。但是,似乎isValid方法正在返回與您所期望的相反的結果。

下面是代碼:

形式:

class Application_Form_Album extends Zend_Form { 

public function init() { 


    ## Set Recapture 


    $this->setName('album'); 
    $this->setMethod('POST'); 
    $id = new Zend_Form_Element_Hidden('id'); 
    $id->addFilter('Int'); 
    $artist = new Zend_Form_Element_Text('artist'); 
    $artist->setLabel('Artist') 
      ->setRequired(true) 
      ->addFilter('StripTags') 
      ->addFilter('StringTrim') 
      ->addValidator('NotEmpty'); 
    $title = new Zend_Form_Element_Text('title'); 
    $title->setLabel('Title') 
      ->setRequired(true) 
      ->addFilter('StripTags') 
      ->addFilter('StringTrim') 
      ->addValidator('NotEmpty'); 
    $submit = new Zend_Form_Element_Submit('submit'); 
    $submit->setAttrib('id', 'submitbutton'); 


    //Change theme 
    $recaptcha = new Zend_Service_ReCaptcha("XXXXXXX","XXXXXXX"); 
    $recaptcha->setOption('theme', 'clean'); 
    $captcha = new Zend_Form_Element_Captcha('challenge', array('captcha' => 'ReCaptcha','captchaOptions' => array('captcha' => 'ReCaptcha','service' => $recaptcha))); 



    $this->addElements(array($id, $artist, $title, $captcha, $submit)); 
} 

}

和控制器方法:

public function addAction() 
{ 



    $auth = Zend_Auth::getInstance(); 
    if ($auth->hasIdentity()) { 
     $form = new Application_Form_Album(); 
     $form->submit->setLabel('Add'); 
     $this->view->form = $form; 
     if ($this->getRequest()->isPost()) { 

       $formData = $this->getRequest()->getPost(); 
       if ($form->isValid($formData)) { 

        $captcha = new Zend_Service_ReCaptcha("XXXXXXX","XXXXXXX"); 
        $result = $captcha->verify($this->_getParam('recaptcha_challenge_field'), 
               $this->_getParam('recaptcha_response_field')); 

        if ($result->isValid()) { 
         //ReCaptcha validation error 
         #echo "CAPTCHA FAILED!<br>"; 

        } else { 
         $artist = $form->getValue('artist'); 
         $title = $form->getValue('title'); 
         $albums = new Application_Model_DbTable_Albums(); 
         $albums->addAlbum($artist, $title); 
         $this->_helper->redirector('index'); 
        } 


       } else { 
        $form->populate($formData); 
       } 
     } 
    } else { 
     $this->_helper->redirector('index','auth'); 
    } 

} 

我會假設($ result->的isValid( ))在輸入有效的驗證碼時返回TRUE。一些頭髮拉動後,我認爲$ result-> isValid()在驗證碼成功輸入時返回FALSE,如果輸入了錯誤的單詞或沒有輸入單詞,則返回TRUE?

我錯過了什麼嗎?有人知道爲什麼會發生這種情況?

回答

0

我不認爲你需要在你的控制器中創建一個新的Zend_Service_ReCaptcha。表格應該照顧它。嘗試從表單中獲取驗證碼元素,然後查看THAT是否有效,而不是if ($result->isValid()) {。參加http://framework.zend.com/manual/en/zend.captcha.operation.html

看看例如,它可能看起來像:

if ($form->getElement('challenge')->isValid() {