4
votes

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");
2
how is your Captcha element being displayed in your view file? - Haroon
Is that the code in the view file or is it the HTML source of the page ? - Haroon
my view code is just echo $this->form This is the source code, automatically built by zend - mokk
ok, please post the AJAX call with the serialize code, so we can see what is happening. - Haroon
The line(your latest edit should not be present), as it indicates you have added a text field? can you please update all of your form code that is present here,after my suggestions. So we can get the full picture of how your code now looks? - Haroon

2 Answers

4
votes

After struggling a lot, here is the solution (I simply had to 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");

Thanks Haroon for your help and your time :)

1
votes

I think you code needs to be like the below, i think in the contactAction.php file the public function contactAction() should handle displaying the form and the action when the form has been posted to ) :

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

Currently your code is generating a new Captcha to confirm against the data input, because you are instantiating the form after the form has been POSTed to. This instantian of the form needs to be done before the form has been posted to, as I have shown in the code example above.

EDIT

Try this:

$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 new code after the edit works for me using Zend Framework version 1.11.1

What version of Zend Framework are you sing and how is you Captcha being displayed in your view file??

When var dumping our your output, for the captcha elment you should expect something similar to the below where "5g4ef" is the data you inputted into the Captcha input element:

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