How to add Captcha to Zend Form?

I need to use Zend Captcha and write the following 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 am facing is that when I show it in the file "form.phtml", for example:

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

The following is displayed:

enter image description here

i.e. It displays 2 text fields.

Please help me in countering this situation. :)

+5
source share
2 answers

You get a second text field because you are using the ViewHelper decorator for the captcha element. Do not set the ViewHelper decorator for the captcha element and it should work.

+5
source

Hi, I am using this code to remove an extra field

$captcha = new Zend_Form_Element_Captcha('captcha',array('label' => 'Write the chars to the field',  
                                                            'captcha' => array(
                                                                'captcha' => 'Image',   
                                                                'wordLen' => 3,
                                                                'timeout' => 300,
                                                                'font' => APPLICATION_PATH.'/../public/font/two.ttf', 
                                                                'imgDir' => APPLICATION_PATH.'/../public/captcha/', 
                                                                'imgUrl' => 'http://localhost/projx/public/captcha/'

                                                            ),
                                            'decorators' => $this->elementDecorators,
                                            ));   
                                            $captcha->removeDecorator('ViewHelper');

this is my decorator

 'decorators' => $this->elementDecorators,

ViewHelpre,

$captcha->removeDecorator('ViewHelper');

, ...:)

0

All Articles