Default Value Validator Problem

I couldn’t find if this was already fixed on new versions but this is what I’m using:

  • Yii 1.1.13

  • Apache 2.2.22

  • Chrome

  • Windows 7

I noticed that if you add under Model Rules a required and default value in that same order the default value is not set until after validation and therefor causing a validation error:




public function rules()

{

// NOTE: you should only define rules for those attributes that

// will receive user inputs.

return array(

       array('user_id, department_id, name, revision, status_id, currency', 'required'),

       array('revision, status_id', 'default', 'value'=>1, 'setOnEmpty'=>TRUE),

       array('user_id', 'default', 'value'=>Yii::app()->user->id, 'setOnEmpty'=>TRUE),

       array('department_id', 'default', 'value'=>Yii::app()->user->DepartmentID, 'setOnEmpty'=>TRUE),

);

}



If I specify the defaults first then validation will pass just fine:




public function rules()

{

// NOTE: you should only define rules for those attributes that

// will receive user inputs.

return array(

       array('revision, status_id', 'default', 'value'=>1, 'setOnEmpty'=>TRUE),

       array('user_id', 'default', 'value'=>Yii::app()->user->id, 'setOnEmpty'=>TRUE),

       array('department_id', 'default', 'value'=>Yii::app()->user->DepartmentID, 'setOnEmpty'=>TRUE),

       array('user_id, department_id, name, revision, status_id, currency', 'required'),

);

}



It seems that the order of defined rules does matter but didn’t see this mentioned anywhere on the CValidator or CDefaultValueValidator Class Reference.

Yes, validation rules are executed in the order they are specified but why would you set a required validator for an attribute when you already assured it will always have a value?

The validators are processed in order, so the order definitely matters. I don’t know whether this is explicitly stated, but it is logical given that you would want certain validation to occur first (such as ‘required’) before checking others (such as ‘unique’).

You also have the skipOnError attribute, which wouldn’t make much sense unless you could define the validation order.

EDIT:

Ninja’d