Yii Framework Forum: Optional Image upload on update - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Optional Image upload on update Rate Topic: -----

#1 User is offline   Bios Element 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 13-February 09
  • Location:Ohio, USA

Posted 26 April 2009 - 10:41 AM

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

0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,880
  • Joined: 04-October 08
  • Location:DC, USA

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.
0

#3 User is offline   Bios Element 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 13-February 09
  • Location:Ohio, USA

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

0

#4 User is offline   Bios Element 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 13-February 09
  • Location:Ohio, USA

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?
0

#5 User is offline   Bios Element 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 13-February 09
  • Location:Ohio, USA

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));
}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users