in old Yii I was using
<?php echo $form->labelEx($model,'text').'<span class="required">* </span>'); ?>
What should I use in yii2 for labeling ?
The Yii2
's way is like below:
<?= $form->field($model, 'fieldName')->label('Label Of FieldName'); ?>
So yours would be something like below:
<?= $form->field($model, 'text')->label('Text'. Html::tag('span', '*',['class'=>'required'])); ?>
Please note that you need to add use yii\helpers\Html;
in your View. Otherwise, you need to replace Html::
with yii\helpers\Html::
.
Update
For those who suffer from required
css class added automatically to parent DIV
of a form field:
You can remove it like below:
$form = ActiveForm::begin(['requiredCssClass' => '' ...
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.
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