Zend - Recaptcha奇怪的问题 - 工作但不如预期

时间:2011-01-09 14:54:21

标签: php zend-framework zend-form captcha recaptcha

我有点奇怪的问题。我正在测试Zend并需要在表单中添加reCaptcha字段。我按照Zend文档中给出的示例,它不起作用(习惯了)。得到'错误的验证码 - 解决'错误。

读了一会儿后,我终于成功了。但是,似乎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));
}

}

Controller方法:

    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-&gt; isValid())在输入的有效验证码上返回TRUE。在一些头发拉动之后,我认为$ result-&gt; isValid()在成功输入验证码时返回FALSE,如果输入了错误的单词或没有单词,则返回TRUE?

我错过了什么吗?任何人都知道为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

我认为您不需要在控制器中创建新的Zend_Service_ReCaptcha。表格应该照顾它。而不是if ($result->isValid()) {,尝试从表单中获取验证码元素,看看它是否有效。请查看http://framework.zend.com/manual/en/zend.captcha.operation.html

中的示例

它可能看起来像:

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