More than one validation rules for a field


I have users table in which, along with other info, I am also saving users’ pictures. Users can register from application registration form or using facebook connect. The application’s registration form have a file field which is used to upload user’s picture. So, in the Model’s validation rules I have specified it as file. following is my rules function for Users model

public function rules()


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

	// will receive user inputs.

	return array(

		array('Username, Name, Email, Passwd', 'length', 'max'=>255),

		array('Role', 'length', 'max'=>5),

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

		// The following rule is used by search().

		// Please remove those attributes that should not be searched.

		array('Id, Username, Name, Email, Passwd, Role', 'safe', 'on'=>'search'),



The problem is that when I save information of a user who registers through facebook connect, I get an error about Picture since its a string in this case. Following is piece of code for inserting facebook user’s record.

                $model = new Users;                   

                $model->Name = $user['name'];

                $model->Source = Users::FACEBOOK;

                $model->OAuth_Uid = $user['id'];

                $model->Status = Users::ACTIVE;

                $model->Role = "user";

                $model->Picture = ''.$user['id'].'/picture';                        




Is there a way to handle this?


I think what you need to do is in your contoller action where user can register manually, you need to set a scenario.

So instead of $model=new Users; change it to something like $model=new Users(‘Form’);

Then in your rules change your picture rule to this:

array(‘Picture’, ‘file’, ‘types’=>‘jpg, gif, png’, ‘on’=>‘Form’),

Thank you GSTAR, this was exactly what I needed. Your reply gave me direction and this article further helped me

Thanks again :)