Issues

ZF-12387: Zend Form Element objects added via addElement() don't get default element Decorators

Description

When adding an element to a form via $form->addElement(...) it wont get the default element decorators if the argument is an Zend_Form_Element object. E.g.


$form = new Zend_Form();
$form->setElementDecorators(array('viewHelper','Errors'));
$el = new Zend_Form_Element_Text('foo');
$form->addElement($el);
var_dump($form->getElement('foo')->getDecorators());
//vs
$form->addElement('text','bar');
var_dump($form->getElement('bar')->getDecorators());

Could be linked to: http://framework.zend.com/issues/browse/ZF-8942

Comments

There is a problem:


$element = new Zend_Form_Element_Text('foo');
$element->setDecorators(array('ViewHelper', 'Label');

$form->addElement($element);

If we add the element decorators from the form, then we override the current decorators of the element. Bad idea!

I agree that explicitly set decorators should take precedence over the form element defaults, but it makes sense to be able to change the default set of decorators that are used for elements without explicitly set decorators. The alternative is (if you want to use the object method) to add the decorators to each element every time, which is wasteful. Is there a way of inspecting the element object to see if decorators were set, and if not, set the form default?

when an element is created by this code $el = new Zend_Form_Element_Text('foo');

default decorators(ViewHelper, Errors,....) added by default, if decorator option is not specified in the constructor.

Better to create your own factory for elements creation

@[~samadams83] {quote}Is there a way of inspecting the element object to see if decorators were set, and if not, set the form default?{quote} Yes. I will look into it.

Patch and unit tests added.

Patch looks good to me. Please commit.

Fixed on trunk (25222) and release-1.12 (25223)