【问题标题】:Zend decorator - Add css class on field element?Zend 装饰器 - 在字段元素上添加 css 类?
【发布时间】:2014-06-01 12:55:01
【问题描述】:

我正在尝试使用 zend 装饰器来使用自定义容器并在我的元素上添加 css 类。

$form->setElementDecorators(array(
    'viewHelper',
    'Errors',
    array('Label'),
    array(
        array('row'=>'HtmlTag'),
        array('tag'=>'div', 'class'=>'col-md-6')
    )
));
$form->setDecorators(array(
    'FormElements',
    array(
        array('data'=>'HtmlTag'),
        array('tag'=>'div', 'class'=>'row')
    ),
    'Form'
));

有没有办法直接在我的输入上添加一个 css 类? <input class="form-control">

有没有办法将标签和输入封装在2个div中?

其实我有的是

<div class="col-md-6">
    <label></label>
    <input>
</div>

我想要的是

<div class="col-md-6">
    <div class="form-group">
        <label></label>
        <input class="form-control">
    </div>
</div>

我还可以在哪里找到有关要传递给 setElementDecorators() 函数的数组的文档?

谢谢

【问题讨论】:

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


    【解决方案1】:

    尝试像这样添加HtmlTag装饰器:

    $form->setElementDecorators(array(
        'viewHelper',
        'Errors',
        array('Label'),
        array(
            array('row'=>'HtmlTag'),
            array('tag'=>'div', 'class'=>'form-group'),        
        ),
        array('HtmlTag', array('tag'=>'div', 'class'=>'col-md-6')),
    ));
    

    对于所有元素,您可以像这样添加一个类:

    设置 form-control 类的示例:

    foreach($form->getElements() as $element){
        $element->setAttrib('class', 'form-control');
    }
    

    添加 form-control 类的示例:

    foreach($form->getElements() as $element){
        $element->setAttrib('class', 'form-control' . ($element->getAttrib('class') == '' ? '' :  ' ' . $element->getAttrib('class')));
    }
    

    【讨论】:

    • Oups...抱歉,我的“我希望的”html 代码不正确。我只是更新它。
    猜你喜欢
    • 2013-07-25
    • 2011-11-26
    • 2011-05-22
    • 1970-01-01
    • 2012-09-09
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多