priti narang
priti narang

Reputation: 236

Yii scenario not working in module validation

I used the following rules in module:

array('password', 'required','on'=>'insert,userchangepassword'),
array('password', 'compare', 'compareAttribute'=>'cpassword','on'=>'userchangepassword'),
array('cpassword', 'required','on'=>'userchangepassword'),

Here I want to use userchangepassword scenario

Its not triggering on userchangepassword action

Here is my controller:

$model=new Users('userchangepassword');
if(isset($_POST["Users"]))
{
    //print_r($_POST["Users"]);exit;
    $q = 'UPDATE users SET password = "'.$_POST['Users']["password"].'" 
            where id = "'.Yii::app()->user->getId().'"';
    $cmd = Yii::app()->db->createCommand($q);
    $cmd->execute();
    $this->redirect(array('users/useraccount'));
}

$this->render('userrest',array(
    'model'=>$model,
));

Upvotes: 0

Views: 356

Answers (1)

Valentin Rodygin
Valentin Rodygin

Reputation: 864

You need to assign values to model's attributes and call validate to validate user input.

$model = new Users( 'userchangepassword' );

Pay attention, to that fact that you can pass the current scenario in the constructor only if your model is a descendant of CFormModel. Otherwise, you need to call setScenario explicitly:

$model = new Users();
$model->setScenario( 'userchangepassword' );

if( isset( $_POST["Users"] ) ) {
    $model->setAttributes( $_POST["Users"] );
    if ( $model->validate() ) {
        // do something
    }
}

Upvotes: 2

Related Questions