I have created create and Update the ‘Testimonials’ task and it works fine but image update function not works perfectly. I need to load the image to file input when updating a testimonial. How can I do it?
I need to show relevant file name in file input rather than showing no file chosen.
Testimonials view/_form
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'testimonials-form',
'enableClientValidation'=>true,
'enableAjaxValidation'=>false,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
));
?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="control-group">
<?php echo $form->labelEx($model,'testimonials_name'); ?>
<?php echo $form->textField($model,'testimonials_name',array('size'=>60,'maxlength'=>255,'placeholder'=>'Testimonial Name','class'=>'width274')); ?>
<?php echo $form->error($model,'testimonials_name'); ?>
</div>
<div class="control-group">
<?php echo $form->labelEx($model,'testimonials_description'); ?>
<?php echo $form->textArea($model,'testimonials_description',array('size'=>60,'placeholder'=>'Testimonial Description','class'=>'width274')); ?>
<?php echo $form->error($model,'testimonials_description'); ?>
</div>
<div class="control-group">
<?php echo $form->labelEx($model,'image_url'); ?>
<input name="Testimonials[image_url]" id="Testimonials_image_url" type="file" src="http://localhost/nanaska-backend/uploads/testimonials/2693-Screenshot%20(11).png">
<?php echo $form->error($model,'image_url'); ?>
</div>
<?php echo $model['image_url']; ?>
<div class="controls">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array('class'=>'button button-news')); ?>
</div>
<?php $this->endWidget(); ?>
TestimonialsController ActionUpdate method
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(isset($_POST['Testimonials']))
{
$_POST['Testimonials']['image_url'] = $model->image_url;
$model->attributes=$_POST['Testimonials'];
$uploadedFile=CUploadedFile::getInstance($model,'image_url');
if($model->save()){
if(!empty($uploadedFile)) // check if uploaded file is set or not
{
$uploadedFile->saveAs(Yii::app()->basePath.'/../'.$model->image_url);
$this->redirect(array('view','id'=>$model->testimonials_id));
echo "done";
}
}
}
$this->render('update',array(
'model'=>$model,
));
}