title: Yii2 dynamic form (File upload)
body: <p>I want to use dynamic form in yii2(<a href="http://wbraganca.com/yii2extensions" rel="nofollow noreferrer">http://wbraganca.com/yii2extensions</a>) here is my code:
  <a href="https://i.stack.imgur.com/pYIw5.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pYIw5.jpg" alt="enter image description here"></a></p>
  
  <p>part of <strong>controller</strong>:</p>
  
  <pre><code>public function actionUpdate($id)
  {
      $model = $this-&gt;findModel($id);
      $modelsProductImage = $model-&gt;images;
  
      if ($model-&gt;load(Yii::$app-&gt;request-&gt;post())) {
          $oldIDs = ArrayHelper::map($modelsProductImage, 'id', 'id');
          $modelsProductImage = Model::createMultiple(ProductImage::classname(), $modelsProductImage);
          Model::loadMultiple($modelsProductImage, Yii::$app-&gt;request-&gt;post());
          $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsProductImage, 'id', 'id')));
  
          foreach ($modelsProductImage as $index =&gt; $modelProductImage) {
              $modelProductImage-&gt;sort_order = $index;
              $modelProductImage-&gt;product_id = $model-&gt;id;
              //echo $modelProductImage-&gt;file = UploadedFile::getInstance($modelProductImage, "[{$index}]file");
              $file = UploadedFile::getInstanceByName($index);
              print_r($file);
          }
      }    
  }
  </code></pre>
  
  <p>and this is my <strong>view</strong> file:</p>
  
  <pre><code>&lt;?php $form = ActiveForm::begin([
      'id' =&gt; 'dynamic-form',
      'options' =&gt; [
          'enctype' =&gt; 'multipart/form-data'
      ],
  
  ]); ?&gt;
  
  &lt;?php DynamicFormWidget::begin([
      'widgetContainer' =&gt; 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
      'widgetBody' =&gt; '.container-items', // required: css class selector
      'widgetItem' =&gt; '.item', // required: css class
      //'limit' =&gt; 4, // the maximum times, an element can be cloned (default 999)
      'min' =&gt; 1, // 0 or 1 (default 1)
      'insertButton' =&gt; '.add-item', // css class
      'deleteButton' =&gt; '.remove-item', // css class
      'model' =&gt; $modelsProductImage[0],
      'formId' =&gt; 'dynamic-form',
      'formFields' =&gt; [
          'id',
          //'path',
          'product_id',
      ],
  
  ]); ?&gt;
  
  &lt;?php foreach ($modelsProductImage as $index =&gt; $modelProductImage): ?&gt;
      &lt;div class="item panel panel-default col-md-3"&gt;&lt;!-- widgetBody --&gt;
          &lt;div class="panel-heading"&gt;
              &lt;span class="panel-title-address"&gt;&lt;?= Yii::t('app','Image').':'. ($index + 1) ?&gt;&lt;/span&gt;
              &lt;button type="button" class="pull-left remove-item btn btn-danger btn-xs"&gt;&lt;i class="fa fa-minus"&gt;&lt;/i&gt;&lt;/button&gt;
              &lt;div class="clearfix"&gt;&lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="panel-body"&gt;
              &lt;?php
  
              // necessary for update action.
              if (!$modelProductImage-&gt;isNewRecord) {
                  echo Html::activeHiddenInput($modelProductImage, "[{$index}]id");
              }
              ?&gt;
              &lt;?php
              $modelImage = $modelProductImage;
              $initialPreview = [];
              if ($modelImage) {
                  $pathImg =  '/'.$modelImage-&gt;path;
  
                  $initialPreview[] = Html::img($pathImg, ['class' =&gt; 'file-preview-image']);
              }
              ?&gt;
  
              &lt;div class=""&gt;
                  &lt;?= $form-&gt;field($modelProductImage, "[{$index}]file")-&gt;label(false)-&gt;widget(FileInput::classname(), [
                    'options' =&gt; [
                        'multiple' =&gt; false,
                        'accept' =&gt; 'image/*',
                        'class' =&gt; 'productImage-path',
                        'name' =&gt; $index
                    ],
                    'pluginOptions' =&gt; [
                        'previewFileType' =&gt; 'image',
                        'showCaption' =&gt; false,
                        'showUpload' =&gt; false,
                        'browseClass' =&gt; 'btn btn-default btn-sm',
                        //'browseLabel' =&gt; Yii::t('app',' Pick Image'),
                        'browseIcon' =&gt; '&lt;i class="glyphicon glyphicon-picture"&gt;&lt;/i&gt;',
                        'removeClass' =&gt; 'btn btn-danger btn-sm',
                          //'removeLabel' =&gt; ' Delete',
                        'removeIcon' =&gt; '&lt;i class="fa fa-trash"&gt;&lt;/i&gt;',
                        'previewSettings' =&gt; [
                            'image' =&gt; ['width' =&gt; '138px', 'height' =&gt; 'auto']
                        ],
                        'initialPreview' =&gt; $initialPreview,
                        'layoutTemplates' =&gt; ['footer' =&gt; '']
                    ]
                  ]) ?&gt;
                &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p>When i use getInstanceByName in controller it only return one file, and when i use getInstance it return NULL </p>
  
  <p>and when i use getInstanceByname it save new file on old file(not as a new file)</p>