Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 validation rule specific to a scenarios

Tags:

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 ?

like image 385
user7282 Avatar asked Feb 04 '15 06:02

user7282


2 Answers

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

    public function rules()
        {
            return [
                [['first_name', 'email', 'phone', 'password'], 'required', 'except' => 'changepassword'],
                [['password'], 'required', 'on' => 'changepassword']
]}
like image 190
Eduardo Avatar answered Nov 16 '22 12:11

Eduardo


Just specify on property in this validation rule:

['email', 'myvalidation', 'on' => 'login'],
like image 24
arogachev Avatar answered Nov 16 '22 10:11

arogachev