Zend Framework - Zend_Form Decorator Issue

强颜欢笑 提交于 2019-12-04 09:22:24

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?

Hernojack

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!