/** * Render form element * * @param Zend_View_Interface $view * @return string */ public function render(Zend_View_Interface $view = null) { $captcha = $this->getCaptcha(); $captcha->setName($this->getFullyQualifiedName()); if (!$this->loadDefaultDecoratorsIsDisabled()) { // fieldSize decorator mus be first $fieldSize = $this->getDecorator('FieldSize'); $this->removeDecorator('FieldSize'); // duplicated text field generated by ViewHelper decorator $this->removeDecorator('ViewHelper'); $decorators = $this->getDecorators(); $decorator = $captcha->getDecorator(); $key = get_class($this->_getDecorator($decorator, null)); if (!empty($decorator) && !array_key_exists($key, $decorators)) { array_unshift($decorators, $decorator); } $decorator = array('Captcha', array('captcha' => $captcha)); $key = get_class($this->_getDecorator($decorator[0], $decorator[1])); if ($captcha instanceof Zend_Captcha_Word && !array_key_exists($key, $decorators)) { array_unshift($decorators, $decorator); } array_unshift($decorators, $fieldSize); $this->setDecorators($decorators); } $this->setValue($this->getCaptcha()->generate()); return parent::render($view); }
/** * @group ZF-11609 */ public function testIndividualDecoratorsBeforeAndAfterRendering() { // Disable default decorators is true $element = new Zend_Form_Element_Captcha('foo', array('captcha' => 'Dumb', 'captchaOptions' => array('sessionClass' => 'Zend_Form_Element_CaptchaTest_SessionContainer'), 'disableLoadDefaultDecorators' => true, 'decorators' => array('Description', 'Errors', 'Captcha_Word', 'Captcha', 'Label'))); // Before rendering $decorators = array_keys($element->getDecorators()); $this->assertSame(array('Zend_Form_Decorator_Description', 'Zend_Form_Decorator_Errors', 'Zend_Form_Decorator_Captcha_Word', 'Zend_Form_Decorator_Captcha', 'Zend_Form_Decorator_Label'), $decorators, var_export($decorators, true)); $element->render(); // After rendering $decorators = array_keys($element->getDecorators()); $this->assertSame(array('Zend_Form_Decorator_Description', 'Zend_Form_Decorator_Errors', 'Zend_Form_Decorator_Captcha_Word', 'Zend_Form_Decorator_Captcha', 'Zend_Form_Decorator_Label'), $decorators, var_export($decorators, true)); // Disable default decorators is false $element = new Zend_Form_Element_Captcha('foo', array('captcha' => 'Dumb', 'captchaOptions' => array('sessionClass' => 'Zend_Form_Element_CaptchaTest_SessionContainer'), 'decorators' => array('Description', 'Errors', 'Captcha_Word', 'Captcha', 'Label'))); // Before rendering $decorators = array_keys($element->getDecorators()); $this->assertSame(array('Zend_Form_Decorator_Description', 'Zend_Form_Decorator_Errors', 'Zend_Form_Decorator_Captcha_Word', 'Zend_Form_Decorator_Captcha', 'Zend_Form_Decorator_Label'), $decorators, var_export($decorators, true)); $element->render(); // After rendering $decorators = array_keys($element->getDecorators()); $this->assertSame(array('Zend_Form_Decorator_Description', 'Zend_Form_Decorator_Errors', 'Zend_Form_Decorator_Captcha_Word', 'Zend_Form_Decorator_Captcha', 'Zend_Form_Decorator_Label'), $decorators, var_export($decorators, true)); }