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])?>
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
there is a simple way and it worked for me
<?= $form->field($model, 'title')->textInput(['class'=>'form-control'])->label('Your Label',['class'=>'label-class']) ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With