-1

Hi everyone i am using FileInput kartik widget in yii2 project and uploading and saving works fine but i am having problem while updating the images

I can get the images path on the update form and create and array and display in as initialpreview in the widget but the problem is that my files field is required field so even there are some values in initialpreview the form gives error to upload image as its required field. So what should i do here?

I only want to give preview of their uploaded pics to the user but if they dont make any changes than i dont want to update anything for images

Following is my view code and model code

return [
        [[ 'price', 'created_date', 'created_by', 'updated_date', 'updated_by','file','address'], 'required'],
        [['price'], 'number'],
        [['address'], 'string', 'max' => 255],
        [['file'], 'file', 'extensions'=>'jpg, gif, png', 'maxFiles' => 4],
    ];


  <?php
   $initalpreview = array();
   foreach($model->adsImages as $images) {
      $initalpreview[] = Html::img('/upload/'.$images->image, ['class'=>'file-preview-image']);
   }

   ?>
 <?=$form->field($model, 'file[]')->widget(FileInput::classname(), [
'options' => ['accept' => 'image/*',
    'multiple'=>true],
    'pluginOptions' => [
        'maxFileCount' => 5,
        'initialPreview'=> $initalpreview,
        'showUpload' => false,
    ]
]); ?>

So what should i do here?

Thank you

Mike Ross
  • 2,753
  • 3
  • 38
  • 81

1 Answers1

0

try this

In Model create a scenario named 'update' and mention the fields that are required when update action works

return [
        [['price', 'created_date', 'created_by','updated_date','updated_by','address'], 'required', 'on' => 'update'],
    ];

Now inside controller action update call the scenario

public function  actionUpdate(){

 //Model Declaration

        $model->scenario = 'update';

  // update code

    }
Bloodhound
  • 2,720
  • 8
  • 31
  • 66