Yii2 validation rule specific to a scenarios

yii2

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' applied only to the login scenario and not at all in other cases.How this can be achieved in Yii2 ?

Best Solution

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

    public function rules()
        {
            return [
                [['first_name', 'email', 'phone', 'password'], 'required', 'except' => 'changepassword'],
                [['password'], 'required', 'on' => 'changepassword']
]}
Related Question