I have 2 requirements: 1) All hidden input elements should be affected without removing standard decorators. 2) This should happen WITHOUT having to specify it on a per-element basis.
All I need is for a CSS class to be attached to the DT & DD tags IF the element type is Zend_Form_Element_Hidden.
I've tried creating custom HtmlTag, DtDdWrapper, and FormElement decorators, but haven't been able to figure out how to do it.
By default they appear this way:
<dt> </dt>
<dd><input type="hidden" name="whatever" value="bling" /></dd>
I would like them to appear this way:
<dt class="hidden"> </dt>
<dd class="hidden"><input type="hidden" name="whatever" value="bling" /></dd>
That way they'll still be where they should be, but they won't interrupt the flow of the document.
Example of what didn't work:
class My_Form_Decorator_DtDdWrapper extends Zend_Form_Decorator_DtDdWrapper
{
public function render($content)
{
if ($this->getElement() instanceof Zend_Form_Element_Hidden)
{
return '<dt class="hidden"> </dt><dd class="hidden">'.$content.'</dd>';
}
else
{
return parent::render($content);
}
}
Based on your desired sample output I'm unclear why you would want to include the
<dt>
on hidden elements, especially since you're not applying any label, they end up becoming needless markup. Following the assumption that the label is unnecessary, you can achieve the desired effect with the following:Both of the above elements will result in the same output with their respective id's. Example:
The
foreach
loop in theloadDefaultDecorators()
method iteratively applies$this->_hiddenElementDecorator
to each hidden form element when the form is built. If you want to apply the hidden element decorator on multiple forms, just create a parent class with the$_hiddenElementDecorator
variable andloadDefaultDecorators()
method in it.However, if you have your heart set on including the label element (
<dt>
) as described in your sample output and applying the 'hidden' class so you end up with:... you will need to extend the
Zend_Form_Decorator_Label
class and override the render() method. Take a look at the comment in theif (null !== $tag)
block:Finally, to apply your new decorator to all of the hidden form elements you will need to add the an element prefix path point to your decorator and re-add the label decorator to the
$_hiddenElementDecorator
array in theTestForm
class:Easy as pie, no?
This will help you : You may also specify any other attributes.