views:

10028

answers:

7

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?

+9  A: 

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)
);
drfloob
Handy! I used it like $this->addElement('hidden','article_id', array('disableLoadDefaultDecorators' => true)); that in my init method.
Adam Benzan
+1  A: 

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" />
...
Andrew
the problem with this approach is that it is not xhtml compliant
Andrew
+8  A: 

I use this

$element->removeDecorator('DtDdWrapper');

to get rid of the dt dd tags around specific elements

dittonamed
I like your approach better. One less line of code.
Andrew
+3  A: 

// 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);
}
+1  A: 

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

Grant Perry
A: 

here is what takeme2web from http://www.phpfreaks.com/forums/index.php?topic=225848.0 suggests

$yourhiddenzendformelement->setDecorators(array('ViewHelper'));

lordspace
A: 

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

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

yanek1988