Optional File Upload Fields

Hi,

I am trying to create a Form (based on a Model and Controller) with three optional file upload fields. I made them of the type file in the Model itself:




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

...



Then I checked wether a file was submitted in the Create/Update action of the controller:




	$cover_image =CUploadedFile::getInstance($model,'cover_image');

	if ((is_object($cover_image) && get_class($cover_image)==='CUploadedFile')) {

		$model->cover_image = $cover_image;

	}

	...

	

	if($model->save()) {

		if(is_object($cover_image)) {

			$model->cover_image->saveAs(Yii::app()->basePath . '/../editions/' . $model->name . "_" . $model->cover_image);

		}

		...

		

		$this->redirect(array('view','id'=>$model->id));

	}



The field is not set as required. Nevertheless, the form validation gives me always a "Cover image cannot be blank" error when submitting. This should not happen. If the file was not set, it should not be saved/updated anything.

How do I do this?

Set allowEmpty to true - http://www.yiiframework.com/doc/api/1.1/CFileValidator#allowEmpty-detail

Thanks, that’s it. Perfect.