Based on the desired pattern, I don’t understand why you want to include <dt> in hidden elements, especially since you don’t apply any label, they become unnecessary markup. Following the assumption that the label is not needed, you can achieve the desired effect with the following:
class TestForm extends Zend_Form { protected $_hiddenElementDecorator = array( 'ViewHelper', array('HtmlTag', array('tag' => 'dd', 'class' => 'hidden')), ); public function init() { $this->addElement('hidden', 'hiddenElement0'); $element = new Zend_Form_Element_Hidden('hiddenElement1'); $this->addElement($element); } public function loadDefaultDecorators() { foreach ($this->getElements() as $element) { if ($element->getType() === "Zend_Form_Element_Hidden") { $element->setDecorators($this->_hiddenElementDecorator); } } parent::loadDefaultDecorators(); } }
Both of the above elements will lead to the same conclusion with their respective identifiers. Example:
<dd class="hidden"> <input type="hidden" name="hiddenElement0" value="" id="hiddenElement0" /> </dd>
The foreach in the loadDefaultDecorators() method iteratively applies $this->_hiddenElementDecorator to every hidden form element when the form is built. If you want to use the hidden element decorator on several forms, just create a parent class with the $_hiddenElementDecorator variable and loadDefaultDecorators() in it.
However, if you have your heart configured, including the label element ( <dt> ), as described in your output example, and using the "hidden" class, so you get:
<dt class="hidden"> </dt> <dd class="hidden"> <input type="hidden" name="hiddenElement0" value="" id="hiddenElement0" /> </dd>
... you need to extend the Zend_Form_Decorator_Label class and override the render () method. Take a look at the comment in the if (null !== $tag) block:
class My_Form_Decorator_Label extends Zend_Form_Decorator_Label { public function render($content) { $element = $this->getElement(); $view = $element->getView(); if (null === $view) { return $content; } $label = $this->getLabel(); $separator = $this->getSeparator(); $placement = $this->getPlacement(); $tag = $this->getTag(); $id = $this->getId(); $class = $this->getClass(); $options = $this->getOptions(); if (empty($label) && empty($tag)) { return $content; } if (!empty($label)) { $options['class'] = $class; $label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options); } else { $label = ' '; } if (null !== $tag) { require_once 'Zend/Form/Decorator/HtmlTag.php'; $decorator = new Zend_Form_Decorator_HtmlTag();
Finally, to apply your new decorator to all elements of the hidden form, you will need to add the path point of the element prefix to your decorator and add the label decorator to the $_hiddenElementDecorator in the TestForm class
class TestForm extends Zend_Form { protected $_hiddenElementDecorator = array( 'ViewHelper', array('HtmlTag', array('tag' => 'dd', 'class' => 'hidden')), // Add back the label element. array('Label', array('tag' => 'dt', 'class' => 'hidden')), ); public function init() { $this->addElement('hidden', 'hiddenElement0'); $element = new Zend_Form_Element_Hidden('hiddenElement1'); $this->addElement($element); } public function loadDefaultDecorators() { foreach ($this->getElements() as $element) { if ($element->getType() === "Zend_Form_Element_Hidden") { $element->setDecorators($this->_hiddenElementDecorator); } } // Add a decorator prefix path pointing to our new nifty decorator. $this->addElementPrefixPath('My_Form_Decorator', '/path/to/Decorator', Zend_Form_Element::DECORATOR); parent::loadDefaultDecorators(); } }
Easy as a pie, no?