基于模型的操作和参数的规则方案

Is it possible to create customize rule for a model in Yii2 with name of action and its parameter.

I know that rules in model can create by on action.

Because I have a model and controller like this :

Model

public function rules()
{
    $rules = [
        [['pre_approved_by', 'pre_approved_date'], 'required', 'on' => 'pre'],
        [['first_approved_by', 'first_approved_date'], 'required', 'on' => 'first'],
    ];
   return $rules;
}

Controller

public function acctionApprove($hierarchy, $id){
  $model = $this->findModel($id);
   if($hierarchy != 1){
       $model->scenario = "pre";
   }else{
       $model->scenario = "first";
   }

}

Please advise

You can create a conditional validation. With this kind of rule you can write your own function.

For example:

['state', 'required', 'when' => function($model) {
    return $model->country == 'USA';
}]

For a detailed explaination, check out the official doc