I have a problem with kartik file-input and the delete button... i have it working when i use one file. But for the multi files - i can't make the trash button working.. Here is my form and my controller action (delete-files).
As my function deleteFiles is the same for another form/controller and works, i think i have a problem with the form here, more than the action...
Any help would be very much appreciated 🙂
Here is my form code
<?php
$allfiles = [];
$initialPreviewConfigAward = [];
if (!$model->isNewRecord)
{
$filesData = ArrayHelper::map(MakerFiles::find()->where(['maker_id' => $model->id,'type'=>$model->gs_type])->all(),'id','file_url');
foreach($filesData as $iKey=>$iVal)
{
$allfiles[] = '/backend/web/'.$iVal;
$initialPreviewConfigAward = [
'caption' => '/backend/web/'.$iVal,
'url' => Url::to(['greensocial/delete-files','id' => $iKey])];
];
}
}
?>
<?= $form->field($upload, 'file_url[]')->widget(FileInput::classname(),
['options' => ['id'=>'award-file','multiple' => true],
'pluginOptions'=>[
'previewFileType' => 'any',
'overwriteInitial'=>false,
'initialPreview'=>$allfiles,
'initialPreviewAsData'=> true,
'initialPreviewConfig' => $initialPreviewConfigAward,
//'showPreview' => true,
// 'deleteUrl'=> Url::to(['maker/delete-files', 'id' => $initialPreviewConfigAward->id]),
'showCaption' => false,
'showRemove' => false,
'showUpload' => false,
],
])->label(false); ?>
Here is my function (in my controller)
public function actionDeleteFiles($id){
$file = MakerFiles::find()->where(['id'=>$id])->one();
$filetodelete = Url::to('@backend/web/').$file->file_url;
if( file_exists ( $filetodelete )) {
unlink( $filetodelete );
if($file->save(false)){
echo json_encode('Fichier supprimé');
};
//return 'fichier supprimé';
}
else { echo json_encode('Unable to delete'); }
}
So, Thank you very much @MichalHynčica, it was the missing [] i forgot... i can now move on the next problem haha
i post the code modified here:
<?php
$allfiles = [];
$initialPreviewConfigAward = [];
if (!$model->isNewRecord)
{
$filesData = ArrayHelper::map(MakerFiles::find()->where(['maker_id' => $model->id,'type'=>$model->gs_type])->all(),'id','file_url');
foreach($filesData as $iKey=>$iVal)
{
$allfiles[] = '/backend/web/'.$iVal;
$initialPreviewConfigAward = [
'caption' => '/backend/web/'.$iVal,
'url' => Url::to(['greensocial/delete-files','id' => $iKey])];
];
}
}
?>
<?= $form->field($upload, 'file_url[]')->widget(FileInput::classname(),
['options' => ['id'=>'award-file','multiple' => true],
'pluginOptions'=>[
'previewFileType' => 'any',
'overwriteInitial'=>false,
'initialPreview'=>$allfiles,
'initialPreviewAsData'=> true,
'initialPreviewConfig' => $initialPreviewConfigAward,
//'showPreview' => true,
// 'deleteUrl'=> Url::to(['maker/delete-files', 'id' => $initialPreviewConfigAward->id]),
'showCaption' => false,
'showRemove' => false,
'showUpload' => false,
],
])->label(false); ?>