How to add custom class to label in Yii2 form field?
Asked Answered
P

2

5

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])?>
Palmer answered 30/1, 2016 at 10:16 Comment(0)
T
22

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

Tuchun answered 30/1, 2016 at 10:32 Comment(0)
L
2

there is a simple way and it worked for me

<?= $form->field($model, 'title')->textInput(['class'=>'form-control'])->label('Your Label',['class'=>'label-class']) ?>
Lepus answered 15/6, 2017 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.