[SOLVED]Scenarios problem - works with safeAttributes() not with rules()

Hi,

I have some problems with scenarios.

I set up my scenario name like this:



if($_POST['authUser']['password'] != ''){


   $user->setAttributes($_POST['User'], 'updatepassword');


}


It works fine with safeAttributes(), but not with rules()!



public function rules()


{


   return array(		   


      array('password_np','length','max'=>3, 'on'=>'updatepassword'),


   );


}

Here works only 'on'=>'update'.

Do you have any idea why?

How do you call validate() or save()?

Quote

How do you call validate() or save()?

This is how I do it:



public function actionUpdate()


{


   $authuser=$this->loadUser();





   if(isset($_POST['User'])){





      //Set scenario


      if($_POST['User']['password'] != ''){


         $authuser->setAttributes($_POST['User'], 'updatepassword');


      }else{


         $authuser->setAttributes($_POST['User'], 'update');


      }	


		


      if($authuser->save()){


         $this->redirect(array('show','id'=>$authuser->id));


      }


			


   }





   $this->render(...............));


}


You should supply 'updatepassword' as a parameter to save().

Quote

You should supply 'updatepassword' as a parameter to save().
^

I thought that we choose the scenario like this:



$authuser->setAttributes($_POST['User'], 'updatepassword');


I've made it like this:



 //Set scenario


if($_POST['User']['password'] != ''){


   $authuser->setAttributes($_POST['User'], 'updatepassword');


   $sc = 'updatepassword';


}else{


   $authuser->setAttributes($_POST['User'], 'update');


   $sc = 'update';


}





if($authuser->validate($sc)){


   if($authuser->save()){


      $this->redirect(...);


   }


}


it works, but I am not sure if it is the right way.

Yes, this is the right. In the upcoming version 1.0.4, you can  set scenario property once and don't need to pass it as parameter everywhere.

Quote

Yes, this is the right. In the upcoming version 1.0.4, you can  set scenario property once and don't need to pass it as parameter everywhere.

Thanks a lot!

Here's a optimization

<?php


//Set scenario


$scenario = ($_POST['User']['password'] != '') ? 'updatepassword' : 'update');





$authuser->setAttributes($_POST['User'], $scenario);








if($authuser->validate($scenario) && $authuser->save()){


      $this->redirect(...);


}