可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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.
回答2:
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)
);
回答3:
I use this
$element->removeDecorator('DtDdWrapper');
to get rid of the dt dd tags around specific elements
回答4:
// 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);
}
回答5:
When you have a lot of hidden inputs best answer is the following:
$elements = $this->getElements();
foreach ($elements as $elem)
if ($elem instanceof Zend_Form_Element_Hidden)
$elem->removeDecorator('label')->removeDecorator('HtmlTag');
回答6:
As mentioned in other posts setDisableLoadDefaultDecorators(true)
doesn't work if they're already loaded... BUT clearDecorators()
does!
回答7:
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" />
...
回答8:
here is what takeme2web from http://www.phpfreaks.com/forums/index.php?topic=225848.0
suggests
$yourhiddenzendformelement->setDecorators(array('ViewHelper'));
回答9:
Using only a single "ViewHelper" decorator will generate invalid markup if you're still using the <dl>
wrapper. Another approach is outlined in ZF-2718. This adds hidden fields to a subform that is wrapped in a <dd>
.
回答10:
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);
}
}
回答11:
Use this:
foreach ($this->getElements() as $element) {
$decorator = $element->getDecorator('label');
if (!$decorator) {
continue;
}
$decorator->removeOption('tag');
}