[solved] $ar->validate( $attributes ? or $scenario ? )

on my model rules:


array('password', 'required', 'on'=>'createUser')

[color="#FF0000"]it cannot work for:[/color]


if($user->validate('createUser'))

i read the source code , is CModel::validate($attributes)

so, i try to:


array('password', 'required', 'on'=>'insert')

[color="#2E8B57"]it can work for:[/color]


if($user->validate($_POST['User'])

however, i think a best way to keep the "scenario" effect~[size="4"][/size]


if($user->validate($_POST['User'], 'scenario'=>'createUser')

it will be better to develop feature~ i think~

You need to set the scenario before validating your model, like this:




$user->scenario = 'createUser';

$user->attributes = $_POST['User'];

$user->validate();



See the bottom part of http://www.yiiframework.com/doc/guide/form.model

read CActiveRecord::__construct($scenario=‘insert’)

discover the scenario parameter is set default by construct~

and then, it can be set in anywhere by $ar->setScenario(‘createUser’)

and then, work in rules to set ‘on’ parameter~~~

[color="#C0C0C0"]

on 1.1, the scenario is private variable~

so, you must use $user->setScenario(‘createuser’);

but, i found, your code is work too, in confusion, how Yii to work…[color="#FF8C00"][/color] :mellow:[/color]

[color="#2E8B57"]

i found in the __set magic function,

it will be trigger the setScenario() function~[/color]

Yii implicitly calls $user->setScenario() whenever you try to write to its scenario property. This works for properties with any name. For example, in your User model:




private $someProperty;

public function setSomeProperty($value) {

    $this->somePropery = $value;

    // maybe do something else here too

}



Now if you try to set someProperty like this:


$user->someProperty = 'test';

The above function will be called by Yii.

Same thing for reading a property, Yii will implicitly call $user->getScenario(), $user->getSomeProperty(), or something similar, whenever this method exists.

It’s explained here: http://www.yiiframework.com/doc/guide/basics.component