in old Yii I was using
<?php echo $form->labelEx($model,'text').'<span class="required">* </span>'); ?>
What should I use in yii2 for labeling ?
in old Yii I was using
<?php echo $form->labelEx($model,'text').'<span class="required">* </span>'); ?>
What should I use in yii2 for labeling ?
You can use this in css instead of modifying code.
div.required label:after {
content: " *";
color: red;
}
That was discussed here: https://github.com/yiisoft/yii2/issues/2056
The
Yii2
's way is like below:So yours would be something like below:
Please note that you need to add
use yii\helpers\Html;
in your View. Otherwise, you need to replaceHtml::
withyii\helpers\Html::
.Update
For those who suffer from
required
css class added automatically to parentDIV
of a form field:You can remove it like below:
Please note that, this applies to your whole form. So whole form has no required
css
class. You need to write it for each field by yourself.