Search code examples
phpzend-frameworkzend-formzend-decorators

ZF1: Add input type as class to tag wrapping input field


Is there a way to add the type of input-element to a class attribute on a wrapping tag? In the example code below it could be the 'Div' decorator that already has the class of 'element' OR the LI-tag.

(I have ommitted some code)

class My_Form extends Zend_Form
{
  public function loadDefaultDecorators($disableLoadDefaultDecorators = false)

    //Set the decorators we need:
    $this->setElementDecorators(array(
        'ViewHelper',
        'Errors',
        array('Description', array('tag' => 'p', 'class' => 'description', 'escape' => false)),
        array('decorator' => array('Div' => 'HtmlTag'), 'options' => array('tag' => 'div', 'class' => 'element')),
        array('Label', array('escape' => false)),        
        array('decorator' => array('Li' => 'HtmlTag'), 'options' => array('tag' => 'li')),        
    ));
  }
}

OR if it's possible to create My_Form_Element, and automaticly have all Zend_Form_Element_XXX extend from that.

I would like to end out with markup like this

<form>
  <ul>
    <li>
      <label for="contactForm-contact_subject" class="optional">Regarding:</label>
      <div class="element form-input-text"><input type="text" name="contactForm[contact_subject]" id="contactForm-contact_subject" value="" /></div>
    </li>
    <li>
      <label for="contactForm-contact_message" class="required">Message:</label>
      <div class="element form-textarea"><textarea name="contactForm[contact_message]" id="contactForm-contact_message" rows="24" cols="80"></textarea></div>
    </li>
    <li>
      <div class="element form-input-submit"><input type="submit" name="contactForm[form_contact_submit]" id="contactForm-form_contact_submit" value="form_contact_submit" /></div>
    </li>
  </ul>
</form>

Solution

  • Just override render method:

    class My_Form extends Zend_Form
    {   
        public function loadDefaultDecorators($disableLoadDefaultDecorators = false)
        {
            //Set the decorators we need:
            $this->setElementDecorators(array(
                'ViewHelper',
                'Errors',
                array('Description', array('tag' => 'p', 'class' => 'description', 'escape' => false)),
                array('decorator' => array('Div' => 'HtmlTag'), 'options' => array('tag' => 'div', 'class' => 'element')),
                array('Label', array('escape' => false)),        
                array('decorator' => array('Li' => 'HtmlTag'), 'options' => array('tag' => 'li')),        
            ));
        }
    
        public function render(Zend_View_Interface $view = null)
        {
            /* @var $element Zend_Form_Element */
            foreach ($this->getElements() as $element) {
                $type = end(explode('_', $element->getType()));
                $element->getDecorator('Div')->setOption('class', 
                    sprintf('%s form-%s', 'element', strtolower($type)));
            }
    
            return parent::render($view);
        }    
    }