Ciprian
Ciprian

Reputation: 3226

yii2 kartik FileInput - File input empty

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,
              ]
                    ]);
                    ?>

Upvotes: 7

Views: 3065

Answers (2)

Touqeer Shafi
Touqeer Shafi

Reputation: 5264

You can use skipOnEmpty on yii2 model rules with update scenarios

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

Upvotes: 2

dungphanxuan
dungphanxuan

Reputation: 615

@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

Upvotes: 2

Related Questions