Validation order changed since version 1.0?


I’m refactoring a bunch of my own code from 1.0.12 to 1.1.3.

Now then, I used to be able to create validation rules on the fly, and depending a bit on class attributes. This doesn’t seem to work anymore; has validate() become somehow static?

To clarify, pseudocode:

public function rules()


   return array(

      array('someAttribute', 'in', 'range'=>$this->createSuitableRange()),



So, previously at least, in $this->createSuitableRange() I was able to rely on the attribute values set for $this object. Now as I’ve changed to 1.1, I’m having trouble doing this. Instead, $this object appears to have empty attribute values within the rules() function.

Is rules() called from a static context after all while validating in 1.1? Documentation seems to claim it non-static.


The validator objects only get created on first access. This can happen quite early, e.g. when you do a massive assignment. Then the safe attributues are searched which in the end triggers createValidators(). It returns a CList of all Validators.

So one idea of a dirty workaround: In beforeValidate you use getValidatorList(), clear the CList, create another list from createValidators() and merge this list back to the first CList. A bit clumsy, though…

Maybe you find a better solution. You should check the source of CModel.

Ugh, kind of dirty, yes, but seems to do the trick. Thanks for the heads-up!

I’ll go with this for a while, as there’s only this one place I need it in. Might look for a better solution if I need to do it again.

Method-based validator should work too. It will look like:

if (!in_array($this->someAttribute, $this->createSuitableRange()))

    $this->addError('my error');

Agree, method based is much better here. Maybe you can refactor your createSuitableRange() right into a validator method.

Take a look at this thread.

Is a bit tedious but there are some good tips