scenario is not working

this is my controller code

public function actionRequest()

{   


     $model1  = new Password();      


    $model->scenario = 'request';


         $this->performAjaxValidation($model1);


 //echo "<pre>"; print_r($model1->scenarios()); var_dump($model1); exit;

if ($model1->load(Yii::$app->request->post()) && $model1->sendRecoveryMessage()) {

        return $this->goBack();


    } else {


        return $this->render('login', [


            'model' => $model,


      ]);


    }


}

below is model code

public function attributeLabels()

{


    return [


        'email'    => \Yii::t('app', 'Email'),


        'password' => \Yii::t('app', 'Password'),


    ];


}





/** @inheritdoc */


public function scenarios()


{


    return [


        'request' => ['email'],


        'reset'   => ['password']


    ];


}


/**


 * @inheritdoc


 */


public function rules()


{


    return  [


              [['email'], 'required'],


             ['email', 'email'],


             [['username'], 'string', 'max'=>200],


             [['email'], 'string', 'max'=>200],


             ['password', 'validator'],


             ['confirmPassword', 'compare','compareAttribute'=>'newPassword', 'message'=>'Passwords not matching'],


            ];


}

and this code print_r($model1->scenarios()); giving me this

Array

(

[request] => Array


    (


        [0] => email


    )





[reset] => Array


    (


        [0] => password


    )

)

if i m using ajax validation without scenario it is working if i m using scenario then it is not working

You have to assign a scenario to your rules





public function rules()

{

return [

    [['email'], 'required', 'on'=>'request'],

    ['email', 'email'],

    [['username'], 'string', 'max'=>200, 'on'=>'request'],

    [['email'], 'string', 'max'=>200, 'on'=>'request'],

    ['password', 'validator', 'on'=>'request'],

    ['confirmPassword', 'compare','compareAttribute'=>'newPassword', 'message'=>'Passwords not matching', 'on'=>'request'],

];

}



how you do your scenarios is better than doing the on=>… if you reuse the rules for more than one scenario (which it looks like you do) because you would have to re-declare all the rules over and over for each scenario.

You have $model1… and for your scenario you have $model->scenario=… you need to have $model1->scenario=…




$model1  = new Password();      

$model->scenario = 'request';


//should be


$model1  = new Password();      

$model1->scenario = 'request';


 //or it can be


$model1  = new Password(['scenario'=>'request']);      

//since it is a new record you can do the above however,

//if it is an update you would have to do 

$model1  = new Password();      

$model1->scenario = 'request';



also make sure you list EVERY attribute that needs to be validated for the specific scenario in the scenario list!