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])?>
halfer
  • 19,824
  • 17
  • 99
  • 186
hd.
  • 17,596
  • 46
  • 115
  • 165

2 Answers2

21

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

Sohel Ahmed Mesaniya
  • 3,344
  • 1
  • 23
  • 29
Chinmay Waghmare
  • 5,368
  • 2
  • 43
  • 68
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']) ?>
Shuhad zaman
  • 3,156
  • 32
  • 32