membedakan rules form update dengan create
#1
Posted 26 April 2012 - 05:16 AM
Jadi intinya bagaimana agar di form update tidak harus mengisi gambar yang akan diupload sedangkan di form create harus memilih gambar..
#2
Posted 26 April 2012 - 06:54 AM
<?php $form = $this->beginWidget('CActiveForm', array(
'id' => 'picture-form',
'htmlOptions' => array(
'enctype' => 'multipart/form-data'
)
));
?>model: 'allowEmpty'=>true
public function rules() {
return array(
array('picture', 'file', 'types'=>'jpg, gif, png', 'allowEmpty'=>true),
// ...
);
}
when i answer, if my answer does not make sense, don't blame on me, coz i am trying to answer to be answered;
ok, i'm covered to mess up and fooling around, yeehaw ...
#3
Posted 26 April 2012 - 09:04 PM
inti dari pertanyaan saya adalah bagaimana cara supaya apabila saya tidak ingin mengganti gambar di form update, maka ketika di klik save, gambar yang ada di filefield tidak berubah menjadi kosong
#4
Posted 30 April 2012 - 09:30 PM
#chadnox
#5
Posted 30 April 2012 - 11:38 PM
ikut mantau jg dah...
#6
Posted 01 May 2012 - 09:52 PM
risdo, on 30 April 2012 - 11:38 PM, said:
ikut mantau jg dah...
wah saya memantau dr kmren blm solved yah,, maaf nih dr kmren mau kasih tau tp nunggu master2. berhubung blm ada yang kasih masukan..
saya coba kasih masukan mungkin gak terbaik,
actionUpdate di controller
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Item']))
{
if(isset($_POST['Item']['img']))
{
$model->attributes=$_POST['Item'];
$sss;
if(strlen(trim(CUploadedFile::getInstance($model,'img'))) > 0)
{
$sss=CUploadedFile::getInstance($model,'img');
$model->img=$model->brand.'.'.$sss->extensionName;
}
if($model->save())
{
if(strlen(trim($model->img)) > 0)
{
$sss->saveAs(Yii::app()->basePath . '/../images/item/'.$model->img);
}
$this->redirect(array('view','id'=>$model->id_item));
}
}
else
{
$model->attributes=$_POST['Item'];
if($model->save())
{
$this->redirect(array('view','id'=>$model->id_item));
}
}
}
$this->render('update',array(
'model'=>$model,));
}
Rules di Model, field img di allow empty (kalo mau bisa update atau create tanpa image di bolehin ya)
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('id_type, brand, desc, price, qty, status', 'required'),
array('id_type, price, qty, status', 'numerical', 'integerOnly'=>true),
array('brand', 'length', 'max'=>255),
array('img', 'file','allowEmpty' => true, 'types'=>'jpg, gif, png'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id_item, id_type, brand, desc, price, qty, status', 'safe', 'on'=>'search'),
);
}
nah terus kalo mau ganti Gambar ane pake algorith ane sendiri jadi ane buat fungsi 1 lagi, yaitu actionDeleteImg yang ane taro di controller, dan di view nya ane buat button nya untuk jalanin fungsi nya.
ini _form nya
<!--FORM jika suatu DATA ada imagenya akan ditampilkan ini -->
<div class="row">
<?php
if(!empty($model->img))
{
echo CHtml::link('Change image',array('deleteImg', 'id'=>$model->id_item));//inibuttonnya
$this->widget('zii.widgets.CDetailView',
array(
'data'=>$model,
'attributes'=>
array(
array(
'label'=>'Current Image',
'type'=>'raw',
'value'=>html_entity_decode(CHtml::image(Yii::app()->baseUrl . '/images/item/'.$model->img,'alt',array('width'=>'300'))),
),
),
));
}
else
{
?>
<div class="row">
<?php echo $form->labelEx($model,'img'); ?>
<?php echo $form->fileField($model,'img'); ?>
<?php echo $form->error($model,'img'); ?>
</div>
<?php
}
?>
?>
</div>
ini fungsi di controller untuk delete dan unlink imagenya
public function actionDeleteImg($id)
{
$model=$this->loadModel($id);
if(!empty($model->img))
{
unlink(Yii::app()->basePath . '/../images/item/'.$model->img);
$model->img ='';
if($model->save(false))
{
$this->redirect(array('update','id'=>$model->id_item));
}
}
else
{
$this->redirect(array('update','id'=>$model->id_item));
}
}
?>
#7
Posted 02 May 2012 - 06:31 AM
public function rules
{
return array(
...........,
array('attributeYgHarusDiIsiSmuaAction/kecualiAttributeImage','required'),
array('image','required','on'=>'create'),
);
}
Terus kalo ada masalah pas action create attribute image tetap bukan required, tambahkan kode ini di function create pada controller,
class ClassName extends Controller
{
........
public function actionCreate()
{
$model=new Image;
//Untuk set scenario yg dibutuhkan Validator, kalo nggak default scenarionya 'insert'
$model->setScenario('create');
}
}
#8
Posted 06 June 2012 - 11:30 PM
#9
Posted 15 June 2012 - 02:30 AM
if(isset($_POST['FileUploadForm'])){
//baris eksekusi klo ada file yang di upload
}
$this->redirect(array("upload")); // ini buat balik lagi klo filenya gak ada
#chadnox
#10
Posted 15 June 2012 - 08:52 PM
Hospital Management Information System
#11
Posted 03 July 2012 - 10:47 AM
saya nemuin masalah sama kaya di atas
teru solusi diatas udah saya ikutin
tapi tetap aja gak bisa
gambarnya tetap hilang jika diupdate dalam keadaan kosong, seharusnya gambarnya tetap ada di database
ada yg bisa ngasih solusi gak ??
plisss
#12
Posted 15 July 2012 - 02:14 PM
rangga, on 26 April 2012 - 09:04 PM, said:
inti dari pertanyaan saya adalah bagaimana cara supaya apabila saya tidak ingin mengganti gambar di form update, maka ketika di klik save, gambar yang ada di filefield tidak berubah menjadi kosong
Mau confirm dulu problemnya:
1. di action create. nama image harus diisi
2. di action update. kalo dikosongin, maka pake data yang sudah di database. kalo diisi, maka upload baru.
kalo bener berarti tinggal pasang variable temporary sebelum attributes di assign terus timpa lagi variablenya kalo kosong.
$model=$this->loadModel($id);
if(isset($_POST['Gallery']))
{
$temp = $model->image;
$model->attributes=$_POST['Gallery'];
$model->image = CUploadedFile::getInstance($model,'image');
if ($model->image->name == "") $model->image = $temp; //tambahin condition seperti ini
if($model->save()) {
if ($model->image != $temp) $model->image->saveAs(Yii::app()->basePath.'/../../image/'.$model->name); //cek kalo kosong atau filenya sama
$this->redirect(array('view','id'=>$model->galleryID));
}
}

Help
















