Zend Framework: How to remove decorators in Zend Form Hidden Element?

I am trying to remove the default decorators in a hidden form element. By default, a hidden item is displayed as follows:

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

I do not want my hidden element to take up space on my page. I want to remove all decoders by default, so all I have left is an input tag.

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

How can i achieve this?

+49
zend-framework zend-form
Jan 26 '09 at 23:51
source share
11 answers

For a hidden field, you need only one decorator - ViewHelper:

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

This will only display an input field without a Dt-Dd shell and label.

+47
Dec 07 2018-10-12T00:
source share

From the Zend Element Decorators Documentation :

No default decorators needed. Uploaded

By default, decorators are loaded by default when the object is initialized. You can disable this by passing the disableLoadDefaultDecorators option to the constructor:

 $element = new Zend_Form_Element('foo', array('disableLoadDefaultDecorators' => true) ); 
+32
Jan 27 '09 at 0:15
source share

I use this

 $element->removeDecorator('DtDdWrapper'); 

to get rid of dt dd tags around certain elements

+24
Feb 10 '09 at 10:15
source share

// based on above is 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); } 
+6
Mar 07 '09 at 15:13
source share

When you have many hidden inputs, the best answer is this:

 $elements = $this->getElements(); foreach ($elements as $elem) if ($elem instanceof Zend_Form_Element_Hidden) $elem->removeDecorator('label')->removeDecorator('HtmlTag'); 
+5
Sep 24 2018-10-12T00:
source share

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

+4
Jul 13 2018-10-13
source share

I could not get disableLoadDefaultDecorators to work perfectly correctly. Here is the solution I came up with.

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

In HTML, a hidden field appears without any additional 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" /> ... 
+3
06 Feb '09 at 18:11
source share

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

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

+2
Jul 24. '10 at 23:56
source share

Using only one ViewHelper decorator will result in invalid markup if you are still using the <dl> wrapper. Another approach is described in ZF-2718 . This adds hidden fields to the subform, which is wrapped in <dd> .

+2
Dec 14 '10 at 22:21
source share

Well, in 2012 and the same question. If you remove the decorators, html will not be checked. If you leave them, hidden elements take up space. In all my projects, I have a CSS.hidden helper, so I just apply it to <dd> and remove the label:

 $element = new Zend_Form_Element_Hidden('foo', array('value' => 'bar')); $element->removeDecorator('Label'); $element->getDecorator('HtmlTag')->setOption('class', 'hidden'); 

Virtual html (5), beautiful looking forms. It may also be part of a custom decorator for hidden fields.

EDIT

This is how I put it in my own form element:

 class Exanto_Form_Element_Hidden extends Zend_Form_Element_Hidden { public function render(Zend_View_Interface $view = null) { $this->removeDecorator('Label'); $this->getDecorator('HtmlTag')->setOption('class', 'hidden'); return parent::render($view); } } 
+2
Jul 31 '12 at 17:32
source share

Use this:

  foreach ($this->getElements() as $element) { $decorator = $element->getDecorator('label'); if (!$decorator) { continue; } $decorator->removeOption('tag'); } 
0
Feb 09 '15 at 11:47
source share



All Articles