yii2 – Scenario in Validation

Using scenario you can perform validation on different situation

Define scenario in model class

    class User extends \yii\db\ActiveRecord 
    {
      public static function tableName()
      {
          return 'user_master';
      }

    // define validation in rule() function
  public function rules()
  {
    return [
      [['email_id'],'email'],
      [['first_name',],'required','on'=>['create','update']],  // create scenario
      [['email_id',],'required','on'=> ['admin','create','update','forgotpassword']],
      [['mobile',],'required','on'=>['admin','create','update']],
    ];
  }
}

Use Scenario in Controller

public function actionCreate()
{
  $model = new User();
  $model->scenario="create";  // use create scenario, create scenario validaion applied in this model
            
  
}
public function actionUpdate()
{
    $model = new User();
    $model->scenario="update";  // use update scenario, update scenario validaion applied in this model
}

if you want to reproduce, please indicate the source:
yii2 – Scenario in Validation - CodeDay