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

Viewed 54716

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?

11 Answers

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)
);

I use this

$element->removeDecorator('DtDdWrapper');

to get rid of the dt dd tags around specific elements

// based on above - a simple function to add a hidden element to $this form

/**
 * Add Hidden Element
 * @param $field
 * @param value
 * @return nothing - adds hidden element
 * */
public function addHid($field, $value){     
    $hiddenIdField = new Zend_Form_Element_Hidden($field);
    $hiddenIdField->setValue($value)
          ->removeDecorator('label')
          ->removeDecorator('HtmlTag');     
    $this->addElement($hiddenIdField);
}

I couldn't get disableLoadDefaultDecorators to work quite right. Here's a solution I came up with.

$hiddenIdField = new Zend_Form_Element_Hidden('id');
$hiddenIdField->setValue($portalId)
              ->removeDecorator('label')
              ->removeDecorator('HtmlTag'); 

In the HTML, the hidden field appears without any extra tags around it.

...
<dt><label for="password" class="required">Password</label></dt>
<dd><input type="password" name="password" id="password" value="" /></dd>
<input type="hidden" name="id" value="1" id="id" />
...

Using only a single "ViewHelper" decorator will generate invalid markup if you're still using the <dl> wrapper. Another approach is outlined in ZF-2718. This adds hidden fields to a subform that is wrapped in a <dd>.

Related