Zend_Form Captcha does not check

I read a few posts about this, but I could not solve my problem. When I try to validate my captcha zend form, it always fails even with the correct text. Here is my code:

// where I call my form

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

// my form

 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') ); } } 

// and my action to 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; } } 

It looks like in my contactAction it is generating a new captcha code, so why it is not consistent with the one I presented, but I have no idea how to fix it.

Thanks for your time and help!

Just saw something tricky: when I reset my $ _POST in the contact action, here is my result:

 array 'strFirstName' => string 'fghjfghj' (length=8) 'strLastName' => string 'ffffffff' (length=8) 'strEmail' => string ' fvhkbno@biu.fr ' (length=14) 'strContent' => string 'fewfew' (length=6) 'captchaField' => string 'cebfe69ead38dba86a6b557dc8853b24' (length=32) 

The captcha I just introduced by an assistant professor even appears, and instead I have a pitching tip !! ??

EDIT

Thanks again for your replay !!! Still not there, even with your changes, but I think I realized that it’s not. Here is my html for the captcha field:

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

When I look at the parameters I sent, this is what I got:

captchaField ab2d15044a637338064b39cfd2675837 captchaField [id] ab2d15044a637338064b39cfd2675837 captchaField [input] 6af7u

CptchaField seems to be overright [id] and [input]

It seems to me that I need to remove this captchaField, but I have no idea how to do it!

I could do it with JS, but there must be a clean way to do it!

CHANGE AGAIN

Im using ajax to submit the form, with serialization. This can be a problem, a sore look.

EDIT TER

It is not caused by ajax. If I manually delete the line:

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

with firebug, everything is fine, and captcha checks well. Now the question is how to delete this line correctly ...

Decision

After much, here is the solution (remove the decorator)!

 $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"); 
+4
source share
2 answers

After much, here is the solution (I just had to remove the decorator)!

 $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"); 

Thanks Haroon for your help and your time :)

+4
source

I think the code should be as below, I think that in the contactAction.php public function contactAction() file should handle the display of the form and the action when the form is 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 } } 

code>

Currently, your code generates a new Captcha to confirm data entry, because you create the form after the form has been submitted. This instance of the form must be completed before the form has been submitted, as I showed in the above code example.

EDIT

Try the following:

 $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); 

code>

This new code after editing works for me using Zend Framework version 1.11.1

What version of Zend Framework are you singing and how do you see Captcha in your view file?

When var offloads our result, for captcha, you should expect something similar to the one below, where "5g4ef" is the data entered into the Captcha input element:

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

+1
source

All Articles