This question come a lot in #ZFtalk channel on IRC.

The Default decorator apply the DtDdWrapper to all form element when they are created. But hidden field does’nt need decorator and the actual hidden element class does’nt care about that.

Actually, without decorator remove, hidden field will output like the following and this is bad.

1
2
<dt id="MyElement-label">&nbsp;</dt>
<dd id="MyElement-element"><input type="hidden" name="MyElement" value="something" id="MyElement"></dd>

So to remove the decorator, you can do this after create your hidden element:

1
2
3
4
5
$id = $form->createElement('hidden', 'id');
$id->removeDecorator('DtDdWrapper');
$id->removeDecorator('HtmlTag');
$id->removeDecorator('Label');
$form->addElement($id);

This is important because this will prevent to get unwanted result if you add css to your DT/Dd decorator. After remove these decorator, this will print only the input hidden field.

1
<input type="hidden" name="MyElement" value="something" id="MyElement">

With the PhenixApp project, I have decided to extends the default hidden element class to remove decorator of each hidden field indefinitely.
I think this way is better because this prevent some method to be executed for nothing and this is more quick to add hidden field in all my form after.

Here is my class :

1
2
3
4
5
6
7
class Phenix_Form_Element_Hidden extends Zend_Form_Element_Hidden
{
    public function init(){
    	$this->setDisableLoadDefaultDecorators(true);
    	$this->addDecorator("ViewHelper");
    }
}

Tags: , , , , , , , , , , , ,

Partager
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • MySpace
  • RSS
  • Live
  • Technorati
  • Twitter
  • Blogosphere News
  • Yahoo! Bookmarks
  • LinkedIn

Related posts:

  1. Autocomplete field with Zend Framework and Dojo

Tags: , , , , , , , , , , , ,

One Response to “Zend_Form : How to remove decorator from hidden field.”

  1. Alex dit :

    Thank you. Thank You! THANK YOU!!
    :)

    There is an open bug in the ZF tracker since 1.5RC for this issue…

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">