Optional Image upload on update

I'm working on a database for an MMO game I play and need to have an optional avatar update when editing an entry. I've got it so that it doesn't actually error but it only saves an edit to the body field if I upload another image for each edit. I know I'm doing something silly but I can't figure it out. >.>

Controller

Model

You can set 'allowEmpty' option for the 'file' validator to be false so that the file attribute can be optional.

Quote

You can set 'allowEmpty' option for the 'file' validator to be false so that the file attribute can be optional.

Thanks qiang. That got me going. I did that and I updated the if statement and added a checkbox. Here's what I did for future reference if anyone does it.

Controller

Model

View _form

Well I've been toying around and am still not quite happy with my workaround. See if the file upload box is empty it'll still try to upload it. Yii itself won't error, It'll just try to upload and rename a non-existent file. I've been googling around but haven't really found a solution. Anyone have any other ideas?

Great news! I've found a perfect solution to this.



	public function actionUpdate()


	{	


		$sentient=$this->loadSentient();


			if(isset($_POST['Sentient']))


			{


			$sentient->attributes=$_POST['Sentient'];


			$sentient->avatar=CUploadedFile::getInstance($sentient,'avatar');


			if($sentient->save())


				if (file_exists($sentient->avatar->getTempName())) {


					$sentient->avatar->saveAs('uploads/avatars/'.$sentient->avatar->getName());


					rename("uploads/avatars/$sentient->avatar", "uploads/avatars/$sentient->id");


				} else {


					unset ($sentient->avatar);


				}


				Yii::app()->user->setFlash('success',"Data saved!");


				$this->redirect(array('show','id'=>$sentient->id));


		}


		$this->render('update',array('sentient'=>$sentient));


	}