Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Captcha in Zend Form?

I need to use Zend Captcha and wrote the follwoing code:

<?php

    require_once ('Zend/Form.php');

    class Form_Signup extends Zend_Form {

        public function __construct( $options = null ) {

            parent::__construct( $options );

            // Set method
            $this->setMethod('post');

            // Elements array
            $elements = array();

            $element = new Zend_Form_Element_Captcha('captcha', array('label'   => "",
                                                                      'captcha' => array('captcha' => 'Image',
                                                                                         'name'    => 'myCaptcha',  
                                                                                         'wordLen' => 5,  
                                                                                         'timeout' => 300,  
                                                                                         'font'    => 'font/monofont.ttf',  
                                                                                         'imgDir'  => 'captcha/',  
                                                                                         'imgUrl'  => '/captcha/')
                                                                     )
                                                    );
            $elements[] = $element;

            // Submit Button
            $element = $this->CreateElement('submit', 'Signup');
            $element->setLabel('Signup');
            $elements[] = $element;

            // --------------------------
            // Add elements to the form
            // --------------------------

            // update tabindex
            foreach ($elements as $index => $element) {
                $element->setAttrib('tabindex', ($index + 1));
            }

            $this->addElements($elements);
            $this->setElementDecorators(array('ViewHelper'));

            // Set form decorator (what script will render the form)
            $this->setDecorators(array(array('ViewScript' , array('viewScript' => 'signup/form.phtml'))));

        }

    }

?>

The problem I encounter is that when I display it in my "form.phtml" file like:

<?= $this->element->captcha ?>

It displays the following:

enter image description here

i.e. It is displaying 2 textboxes.

Kindly help me in counteracting this situation. :)

like image 803
Ahsan Avatar asked Dec 20 '11 13:12

Ahsan


1 Answers

You're getting the second textfield because you're using the ViewHelper decorator for the captcha element. Don't set the ViewHelper decorator for the captcha element and it should work.

like image 116
Ilians Avatar answered Sep 23 '22 21:09

Ilians