model validations are not working when using on scenarios

Hi all,

I am facing problem in validating model field. I want to validate field ‘on’ scenarios.

I have below code with me.




class Images extends CActiveRecord

{

     public $image;

     public $scan_type;

     public $testing_field;

     public function rules()

     {

	return array(

                array('testing_field,image_name, hide, category', 'required'),

                array('image', 'file', 'types'=>'jpg, gif, png'),

                array('image', 'required','on'=>'create'),

                array('scan_type', 'required','on'=>'scan'),

		array('height, width, size, type, created_on,  hide, category', 'numerical', 'integerOnly'=>true),

		array('image_path', 'length', 'max'=>55),

		array('image_name', 'length', 'max'=>30),

		array('id, image_name, height, width, size, type, created_on, hide, category, pagination', 'safe', 'on'=>'search'),

		);

     }

}



I have two custom fields to this model(testing_field,image) where as testing_field is validating for all the scenarios because i haven’t kept any on scenario, but my image field should validate only on create scenario where as it is not validating on create scenario. Could some one help me out where actually i am doing mistake.

How are u putting scenario?


$image->scenario = 'create';

like that?




 array('testing_field,image_name, hide, category', 'required'),



This code is creating a problem here…

remove testing_filed from this array or make it as a safe attribute or use this value where ever you required. if you will define in the top of the validation rules like you are defining now. then it will always create a problem with your validations.

So,try my suggestion here and give your feedbacks. :)

‘testing_field’ field is added only for testing purpose, i want to confirm that custom fields are actually validating or not. It doesn’t matter for me, what i was facing issue here is, any field not only ‘image’ field but also ‘category’ field, if i use ‘on’ scenario then nothing is validating. In my form for action create these fields are showing as not mandatory even though i specified it as mandatory ‘on’=>‘create’ in model class. You can find the same in attached screen shot.