Order Of Validation Of Model Rules

Cconsider following rules:




return array(

   array('id, status', 'numerical', 'integerOnly'=>true ),

   array('id, product_name, status', 'required', 'on'=>'create'),

   array('product_name', 'required', 'on'=>'update'),

)



Now if is there a model with update scenario, do id and status fields will be associate on $model->validate()? Or that only product_name field will be associate on $model->validate()?

Try to use this code.




return array(

   array('id, product_name, status', 'required', 'on'=>'create'),

   array('product_name', 'required', 'on'=>'update'),

   array('id, status','safe'),

   array('id, status', 'numerical', 'integerOnly'=>true ),

)



i hope you will not get any problem here. Since you are defining a scenario for rules then it will only given attributes while validation.

i hope it will not create any issue for you.

Also, as more you will play with this model validation rules more you will learn about the combination for it.

With considering the following rules:




return array(

    array('username, email, password, status', 'required'),

	array('email, newEmail, newEmail_confirm', 'required', 'on'=>'changeEmail'),

)



On changeEmail scenario only ‘email, newEmail, newEmail_confirm’ attributes are required? and in $model->validate() method only these three attributes will be validate?

No, in documantation You can find:

So if you like only ‘email, newEmail, newEmail_confirm’ to be required, you should use it like :


return array(

    array('username, email, password, status', 'required', 'except'=>'changeEmail'),

        array('email, newEmail, newEmail_confirm', 'required', 'on'=>'changeEmail'),

)