views:

2263

answers:

3

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);
    }
}

I then use that class to create all of 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 have a need to display only this one field (without anything else generated by Zend_Form). So in my view I have this:

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

This works fine, except for it generates the field like this:

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

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

My question is: How can I disable all decorators for this element. I don't need decorators for hidden input elements.

+3  A: 

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.

Barrett Conrad
+3  A: 

Hi, 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'));
        }
    }
harvejs
Thank you! I don't know why I didn't think about overriding the loadDefaultDecorators() function.
leek
+1  A: 

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: http://stackoverflow.com/questions/481871/zend-framework-how-do-i-remove-the-decorators-on-a-zend-form-hidden-element