7
votes

I am using kartik file input widget in a yii2 project.

http://demos.krajee.com/widget-details/fileinput

Everything is fine until the update page is accessed where the already uploaded images for a particular post are displayed using the 'initialPreview' option. The preview works, but when the form is submitted, the file input is empty and the form won't validate. Poster Image (256x376) cannot be blank.

<?php
     echo $form->field($model, 'posterImage')->widget(FileInput::classname(), [
       'options' => [
        'multiple' => true,
        'accept' => 'image/*',
       ],
       'pluginOptions' => [
         'maxImageWidth' => 265,
         'maxImageHeight' => 376,
         'minImageWidth' => 265,
         'minImageHeight' => 376,
         'previewFileType' => 'image',
         'allowedFileExtensions' => [
          'jpg', 'jpeg'
          ],
          'showUpload' => false,
          'maxFileSize' => 200,
          'maxFileCount' => 1,
          'initialPreview' => [
            $model->posterImage ? Html::img($model->posterImage, ['width' => '100%']) : NULL,
           ],
           'initialPreviewConfig' => [
            ['url' => $model->posterImage],
            ],
            'initialPreviewAsData' => false,
            'overwriteInitial' => true,
              ]
                    ]);
                    ?>
2
can you share your model class ? - Touqeer Shafi
I don't think it has to do with the model class(which I can't share). I just want the preview(which works) to be uploaded without having to pick again. - Ciprian
have you check skipOnEmpty on validations rules yiiframework.com/doc-2.0/guide-input-file-upload.html - Touqeer Shafi
That's a good idea. But I have to integrate it in my design. Let me check! - Ciprian

2 Answers

2
votes

You can use skipOnEmpty on yii2 model rules with update scenarios

public function rules()
    {
        return [
            [['posterImage'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg'],
        ];
    }
2
votes

@ciprian initialPreview only show image. And not show File input Object for Upload image. So when you reupload, File object will empty so throw error