0

I have the following code in a form in zend framework application.

$captcha = new Zend_Form_Element_Captcha('captcha', array(
                        'label' => "",  
                        'captcha' => 'image',
                        'captchaOptions' => array(  
                                'captcha' => 'image',  
                                'font'=> APPLICATION_PATH . '/../public_html/assets/fonts/akbar.ttf',
                                'imgDir'=> APPLICATION_PATH . '/../public_html/assets/captcha/',
                                'imgUrl'=> '/assets/captcha/',
                        'wordLen' => 1,
                        'fsize'=>20,
                        'height'=>60,
                        'width'=>200,
                        'gcFreq'=>50,
                        'expiration' => 300)
                        )); 

and the display of the form element is as expected.

When I try to validate the form using the following code it always returns error even if I enter the captcha correctly.

    if($this->getRequest()->isPost()) {
        if($this->view->form->isValid($_POST)) {

Any solution on how to validate it correctly will be of great help.

Thanks Nizam

Nizam
  • 551
  • 1
  • 7
  • 23
  • If you remove captcha element, does everything work OK? – Marcin May 07 '11 at 03:08
  • yes only the validation of captcha fails always. I identified that when form request is made new captcha is generated. But I am not able to understand how to validate it before and igonre validating while within the form. – Nizam May 07 '11 at 07:39

2 Answers2

0

Check this post: Zend Framework: Captcha problem

Basically you have to remove the "viewhelper" from the element. ex.:

$form->getElement('captcha')->removeDecorator("viewhelper");
Community
  • 1
  • 1
Slipstream
  • 13,455
  • 3
  • 59
  • 45
0

I'm going to guess that you're also doing $captcha->generate(); before the isPost() check. The problem with this is that when you submit the form, you're generating a new CAPTCHA before checking the POST data, so the check will always fail because it's validating it against the new CAPTCHA. The solution is simply to move the generate call further down.

I put up a blog post a while back with some code examples of this component, see here - http://tfountain.co.uk/blog/2009/1/6/zend-captcha-image-experiences but the post is a couple of years old now so some things may have changed.

If this doesn't help, please edit your question to include a bit more code so we can see what else might be causing the problem.

Tim Fountain
  • 33,093
  • 5
  • 41
  • 69