Zend_Form_Decorator - How to add the Zend_Form_Element_Hidden attribute?

I have 2 requirements: 1) All hidden input elements must be affected without removing standard decorators. 2) This should happen WITHOUT the need to indicate it on the basis of each element.

All I need to do is bind the CSS class to the DT and DD tags if the element type is Zend_Form_Element_Hidden.

I tried to create custom HtmlTag, DtDdWrapper, and FormElement decoders, but couldn't figure out how to do this.

By default, they are displayed as follows:

<dt>&nbsp;</dt> <dd><input type="hidden" name="whatever" value="bling" /></dd> 

I would like them to look like this:

 <dt class="hidden">&nbsp;</dt> <dd class="hidden"><input type="hidden" name="whatever" value="bling" /></dd> 

Thus, they will still be where they should be, but they will not interrupt the flow of the document.

An example of what does not work:

 class My_Form_Decorator_DtDdWrapper extends Zend_Form_Decorator_DtDdWrapper { public function render($content) { if ($this->getElement() instanceof Zend_Form_Element_Hidden) { return '<dt class="hidden">&nbsp;</dt><dd class="hidden">'.$content.'</dd>'; } else { return parent::render($content); } } 
0
php decorator zend-framework zend-form
Feb 03 '09 at 19:23
source share
2 answers

Based on the desired pattern, I don’t understand why you want to include <dt> in hidden elements, especially since you don’t apply any label, they become unnecessary markup. Following the assumption that the label is not needed, you can achieve the desired effect with the following:

 class TestForm extends Zend_Form { protected $_hiddenElementDecorator = array( 'ViewHelper', array('HtmlTag', array('tag' => 'dd', 'class' => 'hidden')), ); public function init() { $this->addElement('hidden', 'hiddenElement0'); $element = new Zend_Form_Element_Hidden('hiddenElement1'); $this->addElement($element); } public function loadDefaultDecorators() { foreach ($this->getElements() as $element) { if ($element->getType() === "Zend_Form_Element_Hidden") { $element->setDecorators($this->_hiddenElementDecorator); } } parent::loadDefaultDecorators(); } } 

Both of the above elements will lead to the same conclusion with their respective identifiers. Example:

 <dd class="hidden"> <input type="hidden" name="hiddenElement0" value="" id="hiddenElement0" /> </dd> 

The foreach in the loadDefaultDecorators() method iteratively applies $this->_hiddenElementDecorator to every hidden form element when the form is built. If you want to use the hidden element decorator on several forms, just create a parent class with the $_hiddenElementDecorator variable and loadDefaultDecorators() in it.




However, if you have your heart configured, including the label element ( <dt> ), as described in your output example, and using the "hidden" class, so you get:

 <dt class="hidden">&nbsp;</dt> <dd class="hidden"> <input type="hidden" name="hiddenElement0" value="" id="hiddenElement0" /> </dd> 

... you need to extend the Zend_Form_Decorator_Label class and override the render () method. Take a look at the comment in the if (null !== $tag) block:

 class My_Form_Decorator_Label extends Zend_Form_Decorator_Label { public function render($content) { $element = $this->getElement(); $view = $element->getView(); if (null === $view) { return $content; } $label = $this->getLabel(); $separator = $this->getSeparator(); $placement = $this->getPlacement(); $tag = $this->getTag(); $id = $this->getId(); $class = $this->getClass(); $options = $this->getOptions(); if (empty($label) && empty($tag)) { return $content; } if (!empty($label)) { $options['class'] = $class; $label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options); } else { $label = '&nbsp;'; } if (null !== $tag) { require_once 'Zend/Form/Decorator/HtmlTag.php'; $decorator = new Zend_Form_Decorator_HtmlTag(); // Add 'class' => 'hidden' to the <dt> tag decorator options. $decorator->setOptions(array('tag' => $tag, 'class' => 'hidden')); $label = $decorator->render($label); } switch ($placement) { case self::APPEND: return $content . $separator . $label; case self::PREPEND: return $label . $separator . $content; } } } 

Finally, to apply your new decorator to all elements of the hidden form, you will need to add the path point of the element prefix to your decorator and add the label decorator to the $_hiddenElementDecorator in the TestForm class

 class TestForm extends Zend_Form { protected $_hiddenElementDecorator = array( 'ViewHelper', array('HtmlTag', array('tag' => 'dd', 'class' => 'hidden')), // Add back the label element. array('Label', array('tag' => 'dt', 'class' => 'hidden')), ); public function init() { $this->addElement('hidden', 'hiddenElement0'); $element = new Zend_Form_Element_Hidden('hiddenElement1'); $this->addElement($element); } public function loadDefaultDecorators() { foreach ($this->getElements() as $element) { if ($element->getType() === "Zend_Form_Element_Hidden") { $element->setDecorators($this->_hiddenElementDecorator); } } // Add a decorator prefix path pointing to our new nifty decorator. $this->addElementPrefixPath('My_Form_Decorator', '/path/to/Decorator', Zend_Form_Element::DECORATOR); parent::loadDefaultDecorators(); } } 

Easy as a pie, no?

+3
Feb 06 '09 at 10:43
source share

This will help you: You can also specify any other attributes.

 $el = $form->getElement('whatever'); $el->addDecorators( array('ViewHelper', array('HtmlTag',array('tag' => 'dt','class'=>'hidden')), array('Label', array('tag' => 'dd ', 'class'=>'hidden')), )); 
0
Feb 03 '09 at 20:23
source share



All Articles