1

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

}

DavidP
  • 53
  • 9
  • 1
    In your foreach cycle you are overwritting `$initialPreviewConfigAward` variable instead of appending to array. Didn't you forget the `[]` there? Also, you have two `url` keys with same value there. – Michal Hynčica Nov 05 '21 at 10:51
  • Sorry for the two url keys, i was trying something and forgot to delete the line before copy/paste it. I've corrected my post :) – DavidP Nov 05 '21 at 12:40

1 Answers1

0

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); ?>
DavidP
  • 53
  • 9