I\'m trying to remove the default decorators on a hidden form element. By default, the hidden element is displayed like this:
Hidden Element Label
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.
As mentioned in other posts setDisableLoadDefaultDecorators(true)
doesn't work if they're already loaded... BUT clearDecorators()
does!
Use this:
foreach ($this->getElements() as $element) {
$decorator = $element->getDecorator('label');
if (!$decorator) {
continue;
}
$decorator->removeOption('tag');
}
Well, 2012 and still the same issue. If you remove the decorators, the html won't validate. If you leave them, the hidden elements take up space. In all of my projects I have a CSS helper .hidden, so I just apply it to the <dd>
and unset the label:
$element = new Zend_Form_Element_Hidden('foo', array('value' => 'bar'));
$element->removeDecorator('Label');
$element->getDecorator('HtmlTag')->setOption('class', 'hidden');
Valid html(5), nice looking forms. This can also go into a custom decorator for the hidden fields.
EDIT
This is how I put it into 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);
}
}
here is what takeme2web from http://www.phpfreaks.com/forums/index.php?topic=225848.0 suggests
$yourhiddenzendformelement->setDecorators(array('ViewHelper'));
// 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);
}