Script-specific Yii2 validation rule

I have the following rules and scenarios

public function rules(){ return [ [['name','email','password'],'required'], ['email','myvalidation'], ['email','email'], [['name', 'email', 'password'], 'required', 'on' => 'register'], ]; } public function scenarios() { $scenarios = parent::scenarios(); $scenarios['login'] = ['name','password','email'];//Scenario Values Only Accepted return $scenarios; } 

I want the rule 'myvalidation' apply only to the login script, and not in other cases. How can this be done in Yii2 ?

+8
yii2
source share
2 answers

Just specify the on property in this validation rule:

 ['email', 'myvalidation', 'on' => 'login'], 
+12
source share

Remember that you can also use "except". In the example:

  public function rules() { return [ [['first_name', 'email', 'phone', 'password'], 'required', 'except' => 'changepassword'], [['password'], 'required', 'on' => 'changepassword'] ]} 
+9
source share

All Articles