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

How to add custom class to label in Yii2 form field?

我想在 Yii2 的这段代码中添加一个自定义的 class to label 标签,但我不知道如何。

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

尝试:

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

有关详细信息,请参阅此 link

更新:

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

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

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