我有點奇怪的問題。我正在測試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?
我錯過了什麼嗎?有人知道爲什麼會發生這種情況?