Zend Framework: How do I remove the decorators on a Zend Form Hidden Element?

54,639

Solution 1

For hidden field you need only one decorator - ViewHelper:

$field = new Zend_Form_Element_Hidden('id');
$field->setDecorators(array('ViewHelper'));

This will render only the input field, without Dt-Dd wrapper and label.

Solution 2

From the Zend Element Decorators documentation:

Default Decorators Do Not Need to Be Loaded

By default, the default decorators are loaded during object initialization. You can disable this by passing the 'disableLoadDefaultDecorators' option to the constructor:

$element = new Zend_Form_Element('foo', 
    array('disableLoadDefaultDecorators' => true)
);

Solution 3

I use this

$element->removeDecorator('DtDdWrapper');

to get rid of the dt dd tags around specific elements

Solution 4

When you have a lot of hidden inputs best answer is the following:

$elements = $this->getElements();
foreach ($elements as $elem)
    if ($elem instanceof Zend_Form_Element_Hidden)
        $elem->removeDecorator('label')->removeDecorator('HtmlTag');

Solution 5

As mentioned in other posts setDisableLoadDefaultDecorators(true) doesn't work if they're already loaded... BUT clearDecorators() does!

Share:
54,639
Mike
Author by

Mike

Updated on April 29, 2020

Comments

  • Mike
    Mike about 4 years

    I'm trying to remove the default decorators on a hidden form element. By default, the hidden element is displayed like this:

    <dt>Hidden Element Label (if I had set one)</dt>
    <dd><input type="hidden" name="foobar" value="1" id="foobar"></dd>
    

    I don't want my hidden element to take up space on my page. I want to remove all the default decorators so all I'm left with is the input tag.

    <input type="hidden" name="foobar" value="1" id="foobar">
    

    How can I achieve this?

  • Mike
    Mike over 15 years
    the problem with this approach is that it is not xhtml compliant
  • Adam Benzan
    Adam Benzan over 14 years
    Handy! I used it like $this->addElement('hidden','article_id', array('disableLoadDefaultDecorators' => true)); that in my init method.
  • BenMorel
    BenMorel about 12 years
    Using this solution, the <input type="hidden"> field does not display at all for me.