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
Page 1 of 1
Optional Image upload on update
#2
Posted 26 April 2009 - 02:38 PM
You can set 'allowEmpty' option for the 'file' validator to be false so that the file attribute can be optional.
#3
Posted 26 April 2009 - 02:58 PM
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
#4
Posted 04 May 2009 - 12:13 AM
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?
#5
Posted 04 May 2009 - 10:59 AM
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));
}
Share this topic:
Page 1 of 1

Help
This topic is locked










