【问题标题】:ZF1: Add input type as class to tag wrapping input fieldZF1:将输入类型作为类添加到标签包装输入字段
【发布时间】:2013-07-01 21:08:44
【问题描述】:

有没有办法将输入元素的类型添加到包装标签的类属性中? 在下面的示例代码中,它可能是已经具有“元素”类或 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>

【问题讨论】:

    标签: php zend-framework zend-form zend-decorators


    【解决方案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);
        }    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-21
      • 2019-07-17
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      • 2020-04-13
      • 2015-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多