Zend Framework: How do I remove the decorators on a Zend Form Hidden Element?

北城以北 提交于 2019-11-29 19:58:58

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.

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)
);

I use this

$element->removeDecorator('DtDdWrapper');

to get rid of the dt dd tags around specific elements

// 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);
}
yanek1988

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');
Grant Perry

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

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" />
...

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

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

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>.

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);
    }
}

Use this:

    foreach ($this->getElements() as $element) {

        $decorator = $element->getDecorator('label');
        if (!$decorator) {
            continue;
        }
        $decorator->removeOption('tag');
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!