ZF-6830: Allow editing/updating of options for decorators already in a zend_form_elements stack.


Sometimes its particularly useful to be able to change options of individual decorators out of an elements decorator stack without having to re-set the entire thing manually (ie when using a preset list of elements and customising minor aspects for the current output). At the moment the closest thing to this would be to use removeDecorator and then addDecorator with the new options list. (which only works for the item on the end of the stack, not the most useful since the order of the stack is important in this case)

something like $element -> setDecoratorOption($decoratoridentifier, $option) and $element -> removeDecoratorOption($decoratoridentifier, $option)

of course the tricky bit i can see here is that decorators dont really get names, at best they're identifiable by their type and the order of that type in the stack. (ie if the stack was viewhelper,htmltag,label,htmltag to ident the htmltags would be htmltag:0, htmltag:1 etc. not ideal since you cant always guarantee the order of the stack if something else is setting it.)


Try addDecorator without removeDecorator. Options could be changed by addDecorator without breaking the order. If you like,you can change options of decorator it has explicitly-defined name.

hrm, add decorator seems to work pretty admirably at this task (many thanks). its a little clunky though since you have to redo all the options that were a part of that decorator previously, rather than change just the one you want to change. so i think the improvement request still stands?

decorators also seem to have issues when you want to attach a single decorator to the element multiple times since it uses the assoc array to build the stack. is there a design reason for this?

There's a much easier way to do this; simply retrieve the decorator from the element and call the appropriate methods:

$decorator = $element->getDecorator('foo');
$decorator->setOption('bar', 'baz');

As for using the same decorator multiple times, you can't. You can have multiple instances of the same decorator, and the way to do this is via aliases; the methodology is described in the manual. The reason behind this is that the decorators are stateful, and can use different options.