I've read quite some posts about this, but I havn't been able to solve my problem. When I try to validate my zend form captcha 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 send it
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( '[email protected]' );
$this->view->bolSent = $this->_objEmail->send();
}
}
else
$this->view->form = $objForm;
}
}
It seems that in my contactAction, it generates a new captcha code, so that's why it soesnt match with the one I submitted, but I have no idea how to fix it.
Thank you for your time and help !!
Just saw something dodgy : when i dump my $_POST in contact action here is my result :
array
'strFirstName' => string 'fghjfghj' (length=8)
'strLastName' => string 'ffffffff' (length=8)
'strEmail' => string '[email protected]' (length=14)
'strContent' => string 'fewfew' (length=6)
'captchaField' => string 'cebfe69ead38dba86a6b557dc8853b24' (length=32)
The captcha I just entered dosent even appear and instead I have the captcha kay !!??
EDIT
thanks again for your replay !!! Still not there even with your changes but I think I got whats wrong. 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 have a look at my params sent, here is what I got :
captchaField ab2d15044a637338064b39cfd2675837 captchaField[id] ab2d15044a637338064b39cfd2675837 captchaField[input] 6af7u
It seems that cptchaField overwright [id] and [input]
I feel like I need to remove this captchaField but have no idea how so far !
I could do that with JS but there must be a clean way to do so !
EDIT AGAIN
Im using ajax to submit the form, with serialize. That could be the problem, ill have a look.
EDIT TER
It is not caused by ajax. If I manually remove the line :
<input type="text" value="ab2d15044a637338064b39cfd2675837" id="captchaField" name="captchaField">
with firebug, everything is normal and the captcha validates well. Now the question is how to remove this line properly...
SOLUTION
After struggling a lot, here is the solution (remove 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");