Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii2 kartik FileInput - File input empty

Tags:

yii2

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,
              ]
                    ]);
                    ?>
like image 341
Ciprian Avatar asked Sep 06 '16 06:09

Ciprian


1 Answers

You can use skipOnEmpty on yii2 model rules with update scenarios

public function rules()
    {
        return [
            [['posterImage'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg'],
        ];
    }
like image 77
Touqeer Shafi Avatar answered Oct 02 '22 00:10

Touqeer Shafi