How to put red asterisk in front of each required field in yii2

Can any one know how to put asterisk in front of each required fields.

I have lots of fields in the form I want put a red asterisk in front of required field.

I went through these references but unable to understand

need help how use these reference in which file do I need to update the css.

this asterisk is on my forms out of box.

what is your yii version and theme ?

1 Like

Add this Css…

for form required field…




form div.required label.control-label:after {

  content:" * ";

  color:red;

}

4 Likes

in which file do I need to put this code

should you use default yii css file


site.css

?

so, put about code in


site.css

or which css file you might included

Thanks Amit5992 its working

Thank you Amit5992. Very Nice solution!!

I’m using Yii2 Latest release 2.0.45 with bootstrap 4, so i have to change control-label to col-form-label in order to working.

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

This can be handled at the layout - as opposed to each form.

Yii by default adds the “required” class to the fields that are required. See below:

<div class="form-group field-calcmortgage-loantype required">

With that already taken care of, what is left is defining the required class in the layout - see style section. I have added a special HTML character but you can simple add an asterisk if that’s all you desire. And once you do that, you never ever have to worry about marking required fields anymore (as long as you make sure the layout is customized).

<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <?php $this->registerCsrfMetaTags() ?>
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
    <style>
...
...
 
    .required:before { content:" \269D"; color:red;}
 
    </style>
</head>