如何在 Yii2 表单字段中添加自定义类到标签?

Posted

技术标签:

【中文标题】如何在 Yii2 表单字段中添加自定义类到标签?【英文标题】:How to add custom class to label in Yii2 form field? 【发布时间】:2016-05-08 03:10:24 【问题描述】:

我想在 Yii2 的这段代码中添加一个自定义类来标记标签,但我不知道怎么做。

<?= $form->field($model, 'name',[
                    'template' => "label\n<div class='col-md-6'>input</div>\nhint\nerror"])->textInput(['maxlength' => true])?>

【问题讨论】:

【参考方案1】:

试试:

<?= $form->field($model, 'name', [
                    'template' => "label\n<div class='col-md-6'>input</div>\nhint\nerror",
                    'labelOptions' => [ 'class' => 'your_custom_class_name' ]
    ])->textInput(['maxlength' => true])?>

更多详情请参考link。

更新:

更多选项请使用\yii\bootstrap\ActiveField (link) 而不是\yii\widgets\ActiveField

【讨论】:

【参考方案2】:

有一个简单的方法,它对我有用

<?= $form->field($model, 'title')->textInput(['class'=>'form-control'])->label('Your Label',['class'=>'label-class']) ?>

【讨论】:

干净简单!谢谢

以上是关于如何在 Yii2 表单字段中添加自定义类到标签?的主要内容,如果未能解决你的问题,请参考以下文章