Hi there.
I have filefield(foto). When I don't what to change uploadedfile on update and keep old one I get error. How should I change it ? When I upload new file it's ok.
Controller:
Model:
I have filefield(foto). When I don't what to change uploadedfile on update and keep old one I get error. How should I change it ? When I upload new file it's ok.
Controller:
public function actionUpdate($id) { $model=$this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if(isset($_POST['Pupil'])) { $path =Yii::app()->baseUrl.'/../../uploads/'; $model->attributes=$_POST['Pupil']; $model->fullname = $model->firstname.' '.$model->lastname; $model->start_time = $model->start_hour.':'.$model->start_min; $model->end_time = $model->end_hour.':'.$model->end_min; if(isset($_POST['Pupil']['foto'])) { $rnd = rand(0,999999); $length = 10; $chars = array_merge(range(0,9), range('a','z'), range('A','Z')); shuffle($chars); $randomChar = implode(array_slice($chars, 0, $length)); $uploadedFile=CUploadedFile::getInstance($model,'foto'); $fileName = "{$randomChar}-{$rnd}-{$randomChar}-{$uploadedFile}"; // random number + file name $model->foto = $fileName; if($model->save()) $uploadedFile->saveAs(Yii::app()->basePath.'/../../uploads/'.$fileName); $this->redirect(array('view','id'=>$model->pupilId)); } else { //What should I change ? if($model->save()) $this->redirect(array('view','id'=>$model->pupilId)); } } $this->render('update',array( 'model'=>$model, )); }
Model:
public function rules() { return array( array('contract_contractId', 'required'), array('foto','file','types'=>'jpg,png,pdf,doc,dot,docx,docm,dotx,xls,xlsm,xltm,ppt,pptm,ppsx,xlsx','allowEmpty'=>true), array('start_hour, start_min, end_hour, end_min, birth_date, start_time, end_time, foto', 'safe'), array('foto','file','allowEmpty'=>true,'on'=>'update'), ); }