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

后端 未结 11 1645
说谎
说谎 2020-12-23 00:07

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
相关标签:
11条回答
  • 2020-12-23 00:10

    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.

    0 讨论(0)
  • 2020-12-23 00:10

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

    0 讨论(0)
  • 2020-12-23 00:14

    Use this:

        foreach ($this->getElements() as $element) {
    
            $decorator = $element->getDecorator('label');
            if (!$decorator) {
                continue;
            }
            $decorator->removeOption('tag');
        }
    
    0 讨论(0)
  • 2020-12-23 00:15

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-23 00:16

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

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

    0 讨论(0)
  • 2020-12-23 00:19

    // 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);
    }
    
    0 讨论(0)
提交回复
热议问题