Yii Framework Forum: Default Value Validator Problem - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Default Value Validator Problem Rate Topic: -----

#1 User is offline   Mr. Macphisto 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 10-November 13

Posted 10 November 2013 - 09:35 PM

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.
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,353
  • Joined: 12-October 09
  • Location:Croatia

Posted 11 November 2013 - 04:18 AM

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?
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,598
  • Joined: 04-March 10
  • Location:UK

Posted 11 November 2013 - 04:22 AM

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users