I have a class that extends Zend_Form like this (simplified):
class Core_Form extends Zend_Form
{
protected static $_elementDecorators = array(
'ViewHelper',
'Errors',
array('Label'),
array('HtmlTag', array('tag' => 'li')),
);
public function loadDefaultDecorators()
{
$this->setElementDecorators(self::$_elementDecorators);
}
}
I then use that class to create all of my forms:
class ExampleForm extends Core_Form
{
public function init()
{
// Example Field
$example = new Zend_Form_Element_Hidden('example');
$this->addElement($example);
}
}
In one of my views, I have a need to display only this one field (without anything else generated by Zend_Form). So in my view I have this:
<?php echo $this->exampleForm->example; ?>
This works fine, except for it generates the field like this:
<li><input type="hidden" name="example" value=""></li>
This is obviously because I set the element decorators to include HtmlTag: tag => 'li'.
My question is: How can I disable all decorators for this element. I don't need decorators for hidden input elements.
the best place to set it is public function loadDefaultDecorators()
for example like this:
class ExampleForm extends Core_Form
{
public function init()
{
//Example Field
$example = new Zend_Form_Element_Hidden('example');
$this->addElement($example);
}
public function loadDefaultDecorators()
{
$this->example->setDecorators(array('ViewHelper'));
}
}
Reset the decorators for the form element to only use 'ViewHelper'. For example:
<?php echo $this->exampleForm->example->setDecorators(array('ViewHelper')) ; ?>
Obviously, the view is not the ideal place to do this, but you get the idea. Note that calling setDecorator***s***() resets all the decorators instead of adding a new one.
If you disable the dd/dt decorators on the hidden element, you'll have invalid XHTML, because you'll have something that's not a valid item in a dl. The only solution is to disable these decorators on all form elements, not just the hidden ones, and to disable them on the entire form, too. For consistency, you'll want to do this across all forms.
IMHO, this is a bad design decision in ZF. I mean, saying that the value of an input is the "definition" of a "term" is a cute idea semantically, but it's not fully thought-out.
Same question here: Zend Framework: How do I remove the decorators on a Zend Form Hidden Element?
If you are going to add elements in this way:
$this->addElement(
'text',
'a1',
array('required' => true, 'validators' => array('Alpha'))
);
You can get out the dd/dt
tags for every element with this:
$this->setElementDecorators(array('ViewHelper'));
or if you are goint to add elements in this other way:
$nombre1 = new Zend_Form_Element_Text(
'n1',
array('id'=> 'Nombre1', 'validators' => array('Alpha') )
);
//$nombre1->setDecorators(array('ViewHelper'));
$this->addElement($nombre1);
You need to uncomment:
//$nombre1->setDecorators(array('ViewHelper'));
in order to disable the dd/dt
tags.
This last way is only to disable the current element, the others elements in the form keep the <dd>
<dt>
tags as normally.
Here's what I do:
class M_Form_Element_Hidden extends Zend_Form_Element_Hidden {
public function init() {
$this->setDisableLoadDefaultDecorators(true);
$this->addDecorator('ViewHelper');
$this->removeDecorator('DtDdWrapper');
$this->removeDecorator('HtmlTag');
$this->removeDecorator('Label');
return parent::init();
}
}
Then in your Form,
$element = new M_Form_Element_Hidden('myElement');
$this->addElement($element);
Source