Php Zend_表单验证码未验证

Php Zend_表单验证码未验证,php,zend-form,captcha,Php,Zend Form,Captcha,我读过不少关于这方面的帖子,但我还没能解决我的问题。 当我尝试验证我的zend表单验证码时,它总是失败,即使文本正确。 这是我的密码: //我叫我的表格 public function contactAction() { $this->view->form = new Forms_ContactForm(); } //我的表格 class Forms_ContactForm extends Twitter_Form { public function init() {

我读过不少关于这方面的帖子,但我还没能解决我的问题。 当我尝试验证我的zend表单验证码时,它总是失败,即使文本正确。 这是我的密码:

//我叫我的表格

 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( 'mail@gmail.com' );

                $this->view->bolSent = $this->_objEmail->send();
            }
        }
        else
            $this->view->form = $objForm;
    }
}
我刚刚输入的验证码甚至出现了,取而代之的是我的验证码

编辑

再次感谢您的重播!!! 即使有你的改变,我仍然不在那里,但我想我错了。 以下是我的验证码字段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>

我认为您的代码需要如下所示,我认为在contactAction.php文件中,
公共函数contactAction()
应该处理显示表单和表单发布到的操作):


//显示表单

$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
    }        
}

当前,您的代码正在生成一个新的验证码,以根据数据输入进行确认,因为您是在表单发布到之后实例化表单的。表单的这个实例需要在表单发布到之前完成,正如我在上面的代码示例中所示

编辑

试试这个:

编辑后的新代码适用于我使用Zend Framework 1.11.1版

您正在使用哪一版本的Zend Framework?您的验证码如何显示在视图文件中

当您输出时,对于验证码元素,您应该期望类似于下面的内容,其中“5g4ef”是您输入到验证码输入元素中的数据:


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

在苦苦挣扎之后,以下是解决方案(我只需删除decorator)


谢谢哈龙的帮助和时间:)

谢谢你的快速回答。不幸的是,即使按照您所说的方式更改了我的代码,我仍然无法验证它。@mokk np-请告诉我这是否对您有效。我们希望其他人能够知道此问题是否/如何解决。抱歉,到目前为止没有,我编辑了我的帖子以提供更多详细信息。它可能链接到我的ajax调用以提交表单并覆盖数据(请参见编辑部分)您的Captcha元素在视图文件中如何显示?这是视图文件中的代码还是页面的HTML源代码?我的视图代码只是echo$this->form这是源代码,由zendok自动生成,请用序列化代码发布AJAX调用,这样我们就可以看到发生了什么。行(您最近的编辑不应该出现),因为它表明您添加了一个文本字段?根据我的建议,你能更新你所有的表格代码吗。这样我们就可以全面了解您的代码现在的样子了?
 <input type="text" value="ab2d15044a637338064b39cfd2675837" id="captchaField" name="captchaField">
$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");
$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);
$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");