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

11

50

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?

Seaman answered 26/1, 2009 at 23:51 Comment(5)
Same question: #376688Startling
It's actually a different question, but it's very similar.Seaman
Read the actual question - it's the same.Startling
removing all and removing one decorator may be the same if all you have is one decorator, but it's not the same if you have more than one.Soiree
Answered this question hereThad
S
51

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.

Savagism answered 7/12, 2010 at 14:8 Comment(0)
O
32

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)
);
Odds answered 26/1, 2009 at 23:51 Comment(2)
Handy! I used it like $this->addElement('hidden','article_id', array('disableLoadDefaultDecorators' => true)); that in my init method.Retail
Using this solution, the <input type="hidden"> field does not display at all for me.Francesco
H
24

I use this

$element->removeDecorator('DtDdWrapper');

to get rid of the dt dd tags around specific elements

Homogamy answered 10/2, 2009 at 22:15 Comment(0)
O
6

// 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);
}
Oldtimer answered 7/3, 2009 at 15:13 Comment(0)
U
6

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');
Unsnap answered 24/9, 2010 at 12:4 Comment(0)
P
5

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

Peri answered 13/7, 2010 at 13:46 Comment(0)
S
3

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" />
...
Seaman answered 6/2, 2009 at 18:11 Comment(1)
the problem with this approach is that it is not xhtml compliantSeaman
A
2

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

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

Animalism answered 24/7, 2010 at 23:56 Comment(0)
S
2

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

Sympathy answered 14/12, 2010 at 22:21 Comment(0)
D
2

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);
    }
}
Dna answered 31/7, 2012 at 17:32 Comment(0)
N
0

Use this:

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

        $decorator = $element->getDecorator('label');
        if (!$decorator) {
            continue;
        }
        $decorator->removeOption('tag');
    }
Nordrheinwestfalen answered 9/2, 2015 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.