1

有没有办法将输入元素的类型添加到包装标签上的类属性中?在下面的示例代码中,它可能是已经具有“元素”类或 LI 标记的“Div”装饰器。

(我省略了一些代码)

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

或者,如果可以创建 My_Form_Element,并自动让所有 Zend_Form_Element_XXX 从那里扩展。

我想以这样的标记结束

<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>
4

1 回答 1

1

只需覆盖渲染方法:

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);
    }    
}
于 2013-07-03T01:08:01.363 回答