Today i thought about a smart solution for dependend validation. Since we define rules in a method instead of e.g. a public array i thought, we could do something like this:
public function rules()
{
$isOther = $this->type=='other';
return array(
// field 'other_type' is only required if user selected 'type' of 'other'
array('other_type','required'=>$isOther),
...
This does not work as expected. $this->type in the above example is null at the time the expression is evaluated, no matter what was selected in the form.
The reason is, that the validator objects are created (and thus rules() gets called) before the attributes are assigned. This is required in getSafeAttributeNames to find the attributes that are safe to assign. Later, when validate() is called rules() will not get called anymore because the validator objects where already created.
Even though i see, why this is done, this somehow limits the potential flexibility we have with rules() being a method. It would be cool if things like the above would be possible.
Any ideas?