Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii2 labelEx of ActiveForm

in old Yii I was using

    <?php  echo $form->labelEx($model,'text').'<span class="required">* </span>'); ?>

What should I use in yii2 for labeling ?

like image 985
David Avatar asked Dec 14 '22 17:12

David


1 Answers

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.

like image 59
Ali MasudianPour Avatar answered Feb 03 '23 11:02

Ali MasudianPour