ZF-10804: Zend_Form_Decorator_Captcha render duplication issues

Description

I am not sure if this is a bug or just misunderstanding of the functionality. In summary I basically want to add a wrapper or a break in the html of the captcha like this (notice the

&
):



I am using this to create the element:


    $captcha = new Zend_Form_Element_Captcha('Captcha', array(
          'label' => 'Captcha Label',
          'captcha' => 'Image',
          'captchaOptions' => array(
            'captcha' => 'Image',
            'width' => 140,
            'height' => 50,
            'wordLen' => 5,
            'fontSize' => 25,
            'font' => APPLICATION_PATH . '/../public/fonts/BRLNSDB.TTF',
            'imgDir' => APPLICATION_PATH . '/../public/images/captcha',
            'imgUrl' => '/images/captcha/'
          )
        ));
        $captcha->addPrefixPath('My_Form_Decorator', 'My/Form/Decorator/', 'decorator');
        $this->addElement($captcha);

This can be accomplished by creating a Custom Decorator and overwriting the Captcha Decorator something like below :


class My_Form_Decorator_Captcha extends Zend_Form_Decorator_Abstract
{   
   public function render($content)
    {
        $element = $this->getElement();
        $view    = $element->getView();
        $placement = $this->getPlacement();
        $separator = $this->getSeparator();
        $captcha = $element->getCaptcha();

        $markup  = '
' . $captcha->render($view, $element) . '

'; switch ($placement) { case 'PREPEND': $content = $markup . $separator . $content; break; case 'APPEND': default: $content = $content . $separator . $markup; } return $content; } }

Hopefully there would be an easier way to do a simple thing like this but the problem I cant figure out is is if you DO NOT want to overwrite the captcha say calling it something like: My_Form_Decorator_CustomCaptchaStyle instead of My_Form_Decorator_Captcha

Recreating my element below:


    $captcha = new Zend_Form_Element_Captcha('Captcha', array(
          'label' => 'Captcha Label',
          'captcha' => 'Image',
          'captchaOptions' => array(
            'captcha' => 'Image',
            'width' => 140,
            'height' => 50,
            'wordLen' => 5,
            'fontSize' => 25,
            'font' => APPLICATION_PATH . '/../public/fonts/BRLNSDB.TTF',
            'imgDir' => APPLICATION_PATH . '/../public/images/captcha',
            'imgUrl' => '/images/captcha/'
          )
        ));
        $captcha->addPrefixPath('My_Form_Decorator', 'My/Form/Decorator/', 'decorator');
                $captcha->setDecorators(array(
            'CustomCaptchaStyle',
            array('HtmlTag', array('tag' => 'dd')),
            'Label'
        ));
     $this->addElement($captcha);

THE PROBLEM is there is no way of deleting or stopping the Zend_Form_Decorator_Captcha from being rendered which always results to 2 captcha images being displayed like the code below:




Ive also tried $captcha->removeDecorator('Captcha') to no avail;

Comments

I think that the reason for this is that by default Zend_Form_Captcha will add Zend_Form_Decorator_Captcha and so you are basically rendering it twice because both Zend_Form_Decorator_Captcha and My_Form_Decorator_Captcha are being called.

This is not actually a bug so I will mark it as closed, you might have more success querying the mailing list if you are still having issues (form decorators can be a real pain sometimes).

Look also here: ZF-8094