* '); ?> What should...">

Yii2 labelEx of ActiveForm

in old yii i used

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

What should i use in yii2 for marking?

+4
source share
2 answers

The method is Yii2as follows:

<?= $form->field($model, 'fieldName')->label('Label Of FieldName'); ?>

So yours will look something like this:

<?= $form->field($model, 'text')->label('Text'. Html::tag('span', '*',['class'=>'required'])); ?>

Please note that you need to add use yii\helpers\Html;to your view. Otherwise, you need to replace Html::with yii\helpers\Html::.


Update

For those who suffer from requiredthe css class, it is automatically added to DIVthe form field parent element :

You can remove it as shown below:

$form = ActiveForm::begin(['requiredCssClass' => '' ...

, . , css. .

+7

css .

div.required label:after {
    content: " *";
    color: red;
}

: https://github.com/yiisoft/yii2/issues/2056

+5

Source: https://habr.com/ru/post/1568162/


All Articles