Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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])?>
like image 642
hd. Avatar asked Jan 30 '16 10:01

hd.


2 Answers

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

like image 157
Chinmay Waghmare Avatar answered Dec 25 '22 11:12

Chinmay Waghmare


there is a simple way and it worked for me

<?= $form->field($model, 'title')->textInput(['class'=>'form-control'])->label('Your Label',['class'=>'label-class']) ?>
like image 26
Shuhad zaman Avatar answered Dec 25 '22 11:12

Shuhad zaman