title: Update with Yii2-dynamicforms (Nested dynamic forms)
body: <p>In my Yii 2 project, I have a Nested dynamic form working with <a href="https://github.com/wbraganca/yii2-dynamicform" rel="nofollow noreferrer">yii2-dynamicform</a> extension. I could make it work following the <a href="https://github.com/wbraganca/yii2-dynamicform/wiki/Nested-Forms-Example" rel="nofollow noreferrer">wiki guide</a>, but I had to ask for help in <a href="https://stackoverflow.com/questions/32585832/yii-2-nested-forms">this StackOverflow question</a> to make it work properly.
  But I'm having a little problem on Update, when I try to add more fields.</p>
  
  <p>Before explain, let me show my scenario:
  <a href="https://i.stack.imgur.com/oCOW9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oCOW9.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/Bvo7K.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Bvo7K.png" alt="enter image description here"></a></p>
  
  <h2>My problem:</h2>
  
  <p>Updating a record, when I click to Add a new Slot field, instead of adding just one, it adds the number of groups that I have already added.</p>
  
  <p>For example:
  In the image above I have <strong>two</strong> groups (group1 and group2). If i click to add one slot, it adds two (number of groups) slots at once.</p>
  
  <p>Anyone can help me, please?</p>
  
  <h2>My actionUpdate in controller:</h2>
  
  <pre class="lang-php prettyprint-override"><code>public function actionUpdate($id)
  {
  
      $model = $this-&gt;findModel($id);
  
  
      $oldGrupoIds = MissaoGrupo::find()-&gt;select('id')-&gt;where(['missao_id' =&gt; $id])-&gt;asArray()-&gt;all();
      $oldGrupoIds = ArrayHelper::getColumn($oldGrupoIds, 'id');
      $modelsGrupos = MissaoGrupo::findAll(['id' =&gt; $oldGrupoIds]);
      $modelsGrupos = (empty($modelsGrupos)) ? [new MissaoGrupo] : $modelsGrupos;
  
  
      $oldSlotIds = [];
      foreach ($modelsGrupos as $i =&gt; $modelGrupo) {
          $oldSlots = MissaoSlot::findAll(['grupo_id' =&gt; $modelGrupo-&gt;id]);
          $modelsSlots[$i] = $oldSlots;
          $oldSlotIds = array_merge($oldSlotIds, ArrayHelper::getColumn($oldSlots, 'id'));
          $modelsSlots[$i] = (empty($modelsSlots[$i])) ? [new MissaoSlot] : $modelsSlots[$i];
      }
  
  
      if ($model-&gt;load(Yii::$app-&gt;request-&gt;post())) {
  
  
          $modelsGrupos = Model::createMultiple(MissaoGrupo::classname(), $modelsGrupos);
          Model::loadMultiple($modelsGrupos, Yii::$app-&gt;request-&gt;post());
          $newGrupoIds = ArrayHelper::getColumn($modelsGrupos, 'id');
  
  
          $newSlotIds = [];
          $loadsData['_csrf'] =  Yii::$app-&gt;request-&gt;post()['_csrf'];
          $i = 0;
          foreach ($modelsGrupos as $id =&gt; $value) {
              $loadsData['MissaoSlot'] =  Yii::$app-&gt;request-&gt;post()['MissaoSlot'][$i];
              if (!isset($modelsSlots[$id])) {
                  $modelsSlots[$id] = [new MissaoSlot];
              }
              $modelsSlots[$id] = Model::createMultiple(MissaoSlot::classname(), $modelsSlots[$id], $loadsData);
              Model::loadMultiple($modelsSlots[$id], $loadsData);
              $newSlotIds = array_merge($newSlotIds, ArrayHelper::getColumn($loadsData['MissaoSlot'], 'id'));
              $i++;
          }
  
  
          $delSlotIds = array_diff($oldSlotIds, $newSlotIds);
          if (! empty($delSlotIds)) MissaoSlot::deleteAll(['id' =&gt; $delSlotIds]);
          $delGrupoIds = array_diff($oldGrupoIds, $newGrupoIds);
          if (! empty($delGrupoIds)) MissaoGrupo::deleteAll(['id' =&gt; $delGrupoIds]);
  
  
          $valid = $model-&gt;validate();
          $valid = $this-&gt;validaMissao($modelsGrupos, $modelsSlots) &amp;&amp; $valid;
  
          if ($valid) { 
              if ($this-&gt;saveMissao($model, $modelsGrupos, $modelsSlots)) {
                  return $this-&gt;redirect(['view', 'id' =&gt; $model-&gt;id]);
              }
          }
      }
  
      return $this-&gt;render('update', [
          'model' =&gt; $model,
          'modelsGrupos' =&gt; $modelsGrupos,
          'modelsSlots' =&gt; $modelsSlots,
      ]);
  }
  
  protected function saveMissao($modelMissao, $modelsGrupos, $modelsSlots)
  {
      $transaction = \Yii::$app-&gt;db-&gt;beginTransaction();
      try {
          if ($go = $modelMissao-&gt;save(false)) {
  
              foreach ($modelsGrupos as $i =&gt; $modelGrupo) {
                  // Salva os Grupos
                  $modelGrupo-&gt;missao_id = $modelMissao-&gt;id;
                  if ($go = $modelGrupo-&gt;save(false)) {
  
                      foreach ($modelsSlots[$i] as $j =&gt; $modelSlot) {
                          // Salva os Slots
                          $modelSlot-&gt;grupo_id = $modelGrupo-&gt;id;
                          if (! ($go = $modelSlot-&gt;save(false))) {
                              $transaction-&gt;rollBack();
                              break;
                          }
  
                      }
  
                  }
              }
  
          }
          if ($go) {
              $transaction-&gt;commit();
          }
      } catch (Exception $e) {
          $transaction-&gt;rollBack();
      }
  
      return $go;
  }
  </code></pre>
  
  <h2>My Form:</h2>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  use yii\helpers\Html;
  use yii\widgets\ActiveForm;
  use wbraganca\dynamicform\DynamicFormWidget;
  
  /* @var $this yii\web\View */
  /* @var $model app\models\Missao */
  /* @var $form yii\widgets\ActiveForm */
  ?&gt;
  
  &lt;div class="missao-form"&gt;
  
      &lt;?php $form = ActiveForm::begin(['id' =&gt; 'frm_missao']); ?&gt;
  
      &lt;?= $form-&gt;errorSummary($model) ?&gt;
  
      &lt;!-- Nav tabs --&gt;
      &lt;ul class="nav nav-tabs" role="tablist"&gt;
          &lt;li role="presentation" class="active"&gt;
              &lt;a href="#aba-detalhes-missao" aria-controls="home" role="tab" data-toggle="tab"&gt;&lt;i class="fa fa-info-circle"&gt;&lt;/i&gt; Detalhes da missão&lt;/a&gt;
          &lt;/li&gt;
          &lt;li role="presentation"&gt;
              &lt;a href="#aba-slots" aria-controls="profile" role="tab" data-toggle="tab"&gt;&lt;i class="fa fa-users"&gt;&lt;/i&gt; Slots&lt;/a&gt;
          &lt;/li&gt;
      &lt;/ul&gt;
  
      &lt;div class="tab-content"&gt;
          &lt;div role="tabpanel" class="tab-pane active" id="aba-detalhes-missao"&gt;
              &lt;br/&gt;
              &lt;?= $form-&gt;field($model, 'nome')-&gt;textInput(['maxlength' =&gt; true]) ?&gt;
              &lt;?= $form-&gt;field($model, 'desc')-&gt;textInput(['maxlength' =&gt; true]) ?&gt;
          &lt;/div&gt;
          &lt;div role="tabpanel" class="tab-pane" id="aba-slots"&gt;
              &lt;br/&gt;
              &lt;div class="panel panel-default"&gt;
                  &lt;div class="panel-body"&gt;
                      &lt;?php DynamicFormWidget::begin([
                          'widgetContainer' =&gt; 'dynamicform_wrapper',
                          'widgetBody' =&gt; '.container-grupos',
                          'widgetItem' =&gt; '.grupo',
                          //'limit' =&gt; 4,
                          'min' =&gt; 1,
                          'insertButton' =&gt; '.add-grupo',
                          'deleteButton' =&gt; '.remove-grupo',
                          'model' =&gt; $modelsGrupos[0],
                          'formId' =&gt; 'frm_missao',
                          'formFields' =&gt; [
                              'nome',
                          ],
                      ]); ?&gt;
                      &lt;div class="panel panel-default"&gt;
                          &lt;div class="panel-heading"&gt;Grupos / Slots&lt;/div&gt;
                          &lt;div class="panel-body container-grupos"&gt;
                              &lt;?php foreach ($modelsGrupos as $i =&gt; $modelGrupo): ?&gt;
                              &lt;div class="row grupo"&gt; &lt;!-- Item Grupo - INICIO --&gt;
                                  &lt;table class="table"&gt;
                                      &lt;tr&gt;
                                          &lt;th style="width:5%"&gt;#&lt;/th&gt;
                                          &lt;th&gt;Grupo&lt;/th&gt;
                                          &lt;th&gt;Slot&lt;/th&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;&lt;button class="btn btn-danger remove-grupo"&gt;&lt;i class="fa fa-minus"&gt;&lt;/i&gt;&lt;/button&gt;&lt;/td&gt;
                                          &lt;td class="col-md-4"&gt;
                                              &lt;?php
                                              // necessary for update action.
                                              if (! $modelGrupo-&gt;isNewRecord) {
                                                  echo Html::activeHiddenInput($modelGrupo, "[{$i}]id");
                                              }
                                              ?&gt;
                                              &lt;?= $form-&gt;field($modelGrupo, "[{$i}]nome")-&gt;textInput(['maxlength' =&gt; true, 'placeholder' =&gt; 'Nome do Grupo'])-&gt;label(false) ?&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;&lt;!-- Slots --&gt;
                                              &lt;?php DynamicFormWidget::begin([
                                                  'widgetContainer' =&gt; 'dynamicform_inner',
                                                  'widgetBody' =&gt; '.container-slots',
                                                  'widgetItem' =&gt; '.slot',
                                                  //'limit' =&gt; 10,
                                                  'min' =&gt; 1,
                                                  'insertButton' =&gt; '.add-slot',
                                                  'deleteButton' =&gt; '.remove-slot',
                                                  'model' =&gt; $modelsSlots[$i][0],
                                                  'formId' =&gt; 'frm_missao',
                                                  'formFields' =&gt; [
                                                      'nome',
                                                  ],
                                              ]); ?&gt;
                                              &lt;table class="table container-slots"&gt;
                                                  &lt;?php foreach ($modelsSlots[$i] as $j =&gt; $modelSlot): ?&gt;
                                                  &lt;tr class="slot"&gt;
                                                      &lt;td&gt;
                                                          &lt;?php
                                                          // necessary for update action.
                                                          if (! $modelSlot-&gt;isNewRecord) {
                                                              echo Html::activeHiddenInput($modelSlot, "[{$i}][{$j}]id");
                                                          }
                                                          ?&gt;
                                                          &lt;?= $form-&gt;field($modelSlot, "[{$i}][{$j}]nome")-&gt;textInput(['maxlength' =&gt; true, 'placeholder' =&gt; 'Nome do Slot'])-&gt;label(false) ?&gt;
                                                      &lt;/td&gt;
                                                      &lt;td style="width:5%"&gt;&lt;button class="btn btn-danger btn-xs remove-slot"&gt;&lt;i class="fa fa-minus"&gt;&lt;/i&gt;&lt;/button&gt;&lt;/td&gt;
                                                  &lt;/tr&gt;
                                                  &lt;?php endforeach; ?&gt;
                                                  &lt;tfoot&gt;
                                                  &lt;tr&gt;
                                                      &lt;td colspan="2"&gt;&lt;button class="btn btn-info btn-xs add-slot"&gt;&lt;i class="fa fa-plus"&gt;&lt;/i&gt; Novo Slot&lt;/button&gt;&lt;/td&gt;
                                                  &lt;/tr&gt;
                                                  &lt;/tfoot&gt;
                                              &lt;/table&gt;
                                              &lt;?php DynamicFormWidget::end(); ?&gt;
                                          &lt;/td&gt;&lt;!--Slots - fim --&gt;
                                      &lt;/tr&gt;
                                  &lt;/table&gt;
                              &lt;/div&gt; &lt;!-- Item Grupo - FIM --&gt;
                              &lt;?php endforeach; ?&gt;
                          &lt;/div&gt;
                          &lt;div class="panel-footer"&gt;
                              &lt;button class="btn btn-primary btn-xs add-grupo"&gt;&lt;i class="fa fa-plus"&gt;&lt;/i&gt; Novo Grupo&lt;/button&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;?php DynamicFormWidget::end(); ?&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group"&gt;
          &lt;?= Html::submitButton($model-&gt;isNewRecord ? 'Cadastrar' : 'Atualizar', ['class' =&gt; $model-&gt;isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?&gt;
      &lt;/div&gt;
  
      &lt;?php ActiveForm::end(); ?&gt;
  
  &lt;/div&gt;
  </code></pre>