windman
(Mjoszko)
1
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?
qiang
(Qiang Xue)
2
How do you call validate() or save()?
windman
(Mjoszko)
3
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(...............));
}
qiang
(Qiang Xue)
4
You should supply 'updatepassword' as a parameter to save().
windman
(Mjoszko)
5
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.
qiang
(Qiang Xue)
6
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.
jonah
(Poppitypop)
8
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(...);
}