Zend Framework - problem with the Zend_Form decorator

I have a class that extends Zend_Form like this (simplified):

class Core_Form extends Zend_Form { protected static $_elementDecorators = array( 'ViewHelper', 'Errors', array('Label'), array('HtmlTag', array('tag' => 'li')), ); public function loadDefaultDecorators() { $this->setElementDecorators(self::$_elementDecorators); } } 

Then I use this class to create all my forms:

 class ExampleForm extends Core_Form { public function init() { // Example Field $example = new Zend_Form_Element_Hidden('example'); $this->addElement($example); } } 

In one of my views, I only need to display this one field (without anything else created by Zend_Form). Therefore, in my opinion, I have the following:

 <?php echo $this->exampleForm->example; ?> 

This works great, except that it generates a field as follows:

 <li><input type="hidden" name="example" value=""></li> 

This is obviously because I installed the HtmlTag include element decorators: tag => 'li'.

My question is: how can I turn off all decorators for this element. I don't need decorators for hidden input elements.

+4
php zend-framework zend-form
Dec 17 '08 at 10:00
source share
5 answers

The best place to install it is with the public function loadDefaultDecorators ()

for example, for example:

 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')); } } 
+4
Dec 18 '08 at 11:21
source share

Reset decorators for a form element use only "ViewHelper". For example:

 <?php echo $this->exampleForm->example->setDecorators(array('ViewHelper')) ; ?> 

Obviously, an idea is not an ideal place for this, but you get an idea. Note that calling setDecorator *** s *** () resets all decorators instead of adding a new one.

+3
Dec 17 '08 at 23:54
source share

If you disable dd / dt decoders on a hidden element, you will have invalid XHTML because you will have something that is not a valid element in dl. The only solution is to disable these decorators on all elements of the form, not just hidden ones, and also disable them on the entire form. For consistency, you will want to do this in all forms.

IMHO, this is a bad design decision in ZF. I mean, saying that the meaning of input is the "definition" of a "term", it is a pretty idea semantically, but it is not fully thought out.

Same question: Zend Framework: how to remove decorators in a hidden element of a Zend form?

+3
Jun 28 '09 at 17:10
source share

If you are going to add items this way:

 $this->addElement( 'text', 'a1', array('required' => true, 'validators' => array('Alpha')) ); 

You can get dd/dt tags for each element with this:

 $this->setElementDecorators(array('ViewHelper')); 

or if you want to add elements as follows:

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

to disable dd/dt tags. The last way is only to disable the current element, the rest of the elements in the form retain the <dd> <dt> tags as usual.

+1
May 11 '11 at 3:09
source share

That's what I'm doing:

 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

0
Jan 08 '11 at 18:13
source share



All Articles