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

hd. picture hd. · Jan 30, 2016 · Viewed 24.2k times · Source

I want to add a custom class to label tag in this code in Yii2, but I don't know how.

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

Answer

Chinmay Waghmare picture Chinmay Waghmare · Jan 30, 2016

Try:

<?= $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])?>

For more details refer to this link.

UPDATE:

For more options use \yii\bootstrap\ActiveField (link) instead of \yii\widgets\ActiveField