dt102282 2013-07-01 21:08
浏览 15
已采纳

ZF1:将输入类型添加为标记包装输入字段的类

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>
  • 写回答

1条回答 默认 最新

  • du13520157325 2013-07-03 01:08
    关注

    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);
        }    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。