zend-framework - Zend 装饰器 - 在字段元素上添加 css 类?

标签 zend-framework zend-form zend-decorators

我正在尝试使用 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() 函数的数组的文档?

谢谢

最佳答案

尝试添加 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')));
}

关于zend-framework - Zend 装饰器 - 在字段元素上添加 css 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23129979/

相关文章:

php - 无法在 Controller 操作中获取 Bootstrap

zend-framework - 为不同模块设置不同的访问权限

php - 在 .htaccess 或我的 PHP 代码中强制使用 SSL,导致重定向循环错误

php - zend 验证多选框

php - Zend Forms - 元素 ID 修改以允许重复使用

zend-framework - 使用 zend-decorator 将表格列中的 Zend_Form_Element_Radio 格式化为行中的其他 Zend 表单元素

php - 如何让zend在sql中设置time_zone

php - 如何将 id 属性添加到 Zend Framework 表单?

php - 有没有比使用装饰器更好的设计 zend_forms 的方法?

php - 使用 Zend_Form 将多个元素组添加到一个显示组中