wbraganca / yii2-dynamicform

It is widget to yii2 framework to clone form elements in a nested manner, maintaining accessibility.
Other
436 stars 438 forks source link

Multiple File upload is not working #120

Open srbhklkrn opened 8 years ago

srbhklkrn commented 8 years ago

Trying to upload multiple images through two dynamic forms containing each image file in section of the form, but unable to upload it.

swati-clarion commented 8 years ago

I am having same issue. I am having multiple dynamic form in same view page and the multiple image upload is not working. I tried using simple fileInput field and also using Kartik's FileInput widget. Still not working. I always get empty image names after post.

Here is view code:

'dynamicform_wrapper2', // required: only alphanumeric characters plus "_" [A-Za-z0-9_] 'widgetBody' => '.container-items2', // required: css class selector 'widgetItem' => '.item2', // required: css class 'limit' => 7, // the maximum times, an element can be cloned (default 999) 'min' => 1, // 0 or 1 (default 1) 'insertButton' => '.add-item2', // css class 'deleteButton' => '.remove-item2', // css class 'model' => $models1[0], 'formId' => 'dynamic-form', 'formFields' => [ 'detail', 'image', ], ]); ?>
$models1): ?>
```
isNewRecord) { echo Html::activeHiddenInput($models1, "[{$i}]id"); } ?>
field($models1, "[{$i}]detail")->textInput(['maxlength' => true])->label(" STEP {$j}:"); ?>
field($models1, "[{$i}]image")->fileInput()->label("IMAGE {$j}:"); ?> field($models1, "[{$i}]image")->label(false)->widget(FileInput::classname(), [ 'options' => [ 'multiple' => false, 'class' => 'optionvalue-img', 'id' => $id, ], 'pluginOptions' => [ 'previewFileType' => 'image', 'showCaption' => false, 'showUpload' => false, 'browseClass' => 'btn btn-default btn-sm', 'browseLabel' => ' Seleccionar Imagen', 'browseIcon' => '', 'removeClass' => 'btn btn-danger btn-sm', 'removeLabel' => ' Borrar', 'removeIcon' => '', 'previewSettings' => [ 'image' => ['width' => '138px', 'height' => 'auto'] ], 'layoutTemplates' => ['footer' => ''] ] ]) ?>
"; $stepImg = RecipeStep::findOne(['id' => $models1->id]); //echo Html::activeHiddenInput($models1, "[{$i}]image"); //$modelImage = Image::findOne(['id' => $modelOptionValue->image_id]); if ($stepImg->image != '') { echo Html::img(Yii::$app->request->baseUrl.'/recipes/'.$stepImg->image, ['alt'=>'Step Image', 'class'=>'thing', 'height'=>'150px', 'width'=>'150px']); //Html::img($pathImg, ['class' => 'file-preview-image']); } echo "
"; ?>

When i print posted values , I get blank image names:

[RecipeStep] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [detail] => test22ingggg
                    [image] => 
                )
    [1] => Array
        (
            [id] => 3
            [detail] => Test3
            [image] => 
        )

    [2] => Array
        (
            [id] => 4
            [detail] => Test2
            [image] => 
        )

)


This  [image] =>  is always empty after post. I tried everything, but this is not working with image upload.

Please suggest how to sort this.    
swati-clarion commented 8 years ago

We get blank image names in $_POST array but when I print $_FILES I can see the image name array.

salman67 commented 8 years ago

Have you solved this issue? even im facing the same problem. what to do?

swati-clarion commented 8 years ago

Directly use $_FILES array, no other way to sort this. This is code sample of how I fetched uploaded images.

$cnt = 0; foreach ($models1 as $models1) {

//echo "<pre>";
//print_r($models1);

$models1->recipe_id  = $recipeId;

/**** Save step recipe image *****/                                 
 $stepImg = $_FILES['RecipeStep']['name'][$cnt]['image'];
 $tmpImg  = $_FILES['RecipeStep']['tmp_name'][$cnt]['image']; 

  if ($stepImg != '') {                           

        if (move_uploaded_file($tmpImg,"recipes/".$stepImg)) {
            $models1->image = $stepImg;     
        } else {
            Yii::$app->getSession()->setFlash('error', 'Error saving recipe images, Please enter valid images');                                            
        }

  }  //if img        

  if (! ($flag3 = $models1->save(false))) {
    //echo "error for food product1cccc"; die;                               
    $transaction->rollBack();
    break;
 } 

 $cnt++;                                

} //foreach

Hope this helps.

salman67 commented 8 years ago

Im using Yii2 advanced. can i use this in that?

swati-clarion commented 8 years ago

Yes, I am also using yii2 advanced and this worked for me.