Overwritten rules() method


Isn't it possible to access a variable in the rules() method in the model?

E.g for what I mean:

class Model


  public $test;


  function rules()


     echo $this->test;


$testObject = new Model();

$testObject->test = "hello";


Shouldn't it get out "hello"?

When I replace $this->test with "hello" it gives "hello" out.

How is this possible?

Thank you


Do you try to put $test in safeAttributes ?

No, you won't get "hello" because the validator instances are shared among all model instances.

Thank you for the answers

It was just a little example.

The idea is to set for different objects, from one model, different validators.

The Validatos are saved in the DB and it have to get them somehow in the rules() method but for this i need a dynamic ID, which I've to set in the Controller (like in the example)

Is there a possibility to do this or should I search for an other way?


I've done it with a static variable and function… works now, thank you!

Since your validators are dynamic, you may also consider writing a method-based validator. Then refer to CModel::createValidators() to create the validators on-the-fly in the validation method. In this method, $this would refer to the object being validated.