title: Yii2, Modal and form submit
body: <p>I have a Form in Yii2 which contains a button to open a modal with another form.</p>
  
  <p>All the code is below</p>
  
  <p>The issue I am having is that if the form fails validation as I have a unique validator in the model for two attributes, then the page refreshes but into the parent, not into the modal. How would I get the renderAjax to go back to the modal, rather than displaying the data onto the parent page</p>
  
  <p>Sorry for the amount of code and the very vague question, but I didn't know how to explain differently</p>
  
  <p>View for 1st Form</p>
  
  <pre><code>&lt;style&gt;
  div.required label:after {
      content: " *";
      color: red;
  }
  &lt;/style&gt;
  &lt;?php
  use yii\helpers\Html;
  use yii\helpers\Url;
  use yii\helpers\ArrayHelper;
  use kartik\form\ActiveForm;
  use kartik\builder\Form;
  use kartik\builder\FormGrid;
  use kartik\widgets\DepDrop;
  use app\models\Province;
  use app\models\Town;
  
  echo '&lt;p&gt;&lt;span style="color:red"&gt;&lt;strong&gt;*&lt;/strong&gt;&lt;/span&gt; denotes required field.&lt;/p&gt;';
  
  $form = ActiveForm::begin(['type'=&gt;ActiveForm::TYPE_VERTICAL, 'options' =&gt; ['id' =&gt; 'create-workorder-form']]);
  echo FormGrid::widget([
      'model'=&gt;$model,
      'form'=&gt;$form,
      'autoGenerateColumns'=&gt;true,
      'rows'=&gt;[
          [
              'contentBefore'=&gt;'&lt;legend class="text-info"&gt;&lt;small&gt;Address Info&lt;/small&gt;&lt;/legend&gt;',
              'autoGenerateColumns'=&gt;false,
              'columns'=&gt;12,
              'attributes'=&gt;[       // 2 column layout
                  'actions'=&gt;['type'=&gt;Form::INPUT_RAW, 'columnOptions'=&gt;['colspan'=&gt;4], 'value'=&gt;Html::checkbox('use_work_address', 1) . ' ' . Html::label('Use Client Address as Work Address')],
                  'actions1'=&gt;[
                      'type'=&gt;Form::INPUT_RAW, 
                      'columnOptions'=&gt;['colspan'=&gt;2], 
                      'value'=&gt;Html::button('New Work Address', [
                          'value' =&gt; Url::to(['add-address', 'type'=&gt;'work', 'id'=&gt;$model-&gt;id, 'clientId'=&gt;$model-&gt;client_id]), 
                          'title' =&gt; 'Add Work Address for '.$model-&gt;client-&gt;client_name, 
                          'class' =&gt; 'showModalButton btn btn-success',
                          'form' =&gt; 'create-workorder-form',
                          //'data' =&gt; ['method' =&gt; 'post'],
                      ])
                  ],
                  'actions2'=&gt;['type'=&gt;Form::INPUT_RAW, 'columnOptions'=&gt;['colspan'=&gt;4], 'value'=&gt;Html::checkbox('use_work_address', 1) . ' ' . Html::label('Use Client Address as Invoice Address')],
                  'actions3'=&gt;[
                      'type'=&gt;Form::INPUT_RAW, 
                      'columnOptions'=&gt;['colspan'=&gt;2], 
                      'value'=&gt;Html::button('New Invoice Address', [
                          'value' =&gt; Url::to(['add-address', 'type'=&gt;'invoice', 'id'=&gt;$model-&gt;id, 'clientId'=&gt;$model-&gt;client_id]), 
                          'title' =&gt; 'Add Invoice Address for '.$model-&gt;client-&gt;client_name, 
                          'class' =&gt; 'showModalButton btn btn-success',
                          'form' =&gt; 'create-workorder-form',
                          //'data' =&gt; ['method' =&gt; 'post'],
                      ])
                  ],
              ],
          ],     
  
          [
              'attributes'=&gt;[
                  'actions'=&gt;[    // embed raw HTML content
                      'type'=&gt;Form::INPUT_RAW, 
                      'value'=&gt;  '&lt;div style="text-align: right; margin-top: 20px"&gt;' . 
                          Html::resetButton('Reset', ['class'=&gt;'btn btn-default']) . ' ' .
                          Html::submitButton('Submit', ['class'=&gt;'btn btn-primary']) . 
                          '&lt;/div&gt;'
                  ],
              ],
          ]
      ]
  ]);
  ActiveForm::end();
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>public function actionAddAddress($id, $type, $clientId) 
  {
      //Yii::info($_REQUEST, 'c');
      Yii::info(Yii::$app-&gt;request-&gt;referrer, 'c');
      $session = Yii::$app-&gt;session;
      if(isset($_REQUEST['WorkOrders']))
      {
          $session['savedData'] = $_REQUEST['WorkOrders'];
      }
      //Yii::info($session['savedData'], 'c');
      if($type == 'work')
          $model = new \app\models\WorkAddress();
      if($type == 'invoice')
          $model = new \app\models\InvoiceAddress();
  
      $model-&gt;client_id = $clientId;
      if ($model-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $model-&gt;validate()) 
      {
  
          return $this-&gt;redirect([Yii::$app-&gt;request-&gt;referrer]);
      }
      else //(Yii::$app-&gt;request-&gt;isAjax) 
      {
          Yii::info($model-&gt;getErrors(), 'c');
          return $this-&gt;renderAjax('_form_addAddress', [
                      'model' =&gt; $model,
          ]);
      }
  } 
  </code></pre>
  
  <p>Modal Form:</p>
  
  <pre><code>&lt;style&gt;
  div.required label:after {
      content: " *";
      color: red;
  }
  &lt;/style&gt;
  &lt;?php
  use yii\helpers\Html;
  use yii\helpers\Url;
  use yii\helpers\ArrayHelper;
  use kartik\form\ActiveForm;
  use kartik\builder\Form;
  use kartik\builder\FormGrid;
  use yii\widgets\Pjax;
  
  $rows = [
      [
          'contentBefore'=&gt;'&lt;legend class="text-info"&gt;&lt;small&gt;Info&lt;/small&gt;&lt;/legend&gt;',
          'attributes'=&gt;[       // 2 column layout
              'name'=&gt;['type'=&gt;Form::INPUT_TEXT, 'options'=&gt;['placeholder'=&gt;'Enter name...']],
              'company'=&gt;['type'=&gt;Form::INPUT_TEXT, 'options'=&gt;['placeholder'=&gt;'Enter email...']],
          ]
      ],
      [
          'attributes'=&gt;[       // 2 column layout
              'telephone'=&gt;['type'=&gt;Form::INPUT_TEXT, 'options'=&gt;['placeholder'=&gt;'Enter telephone...']],
              'mobile'=&gt;['type'=&gt;Form::INPUT_TEXT, 'options'=&gt;['placeholder'=&gt;'Enter mobile...']],
              'email'=&gt;['type'=&gt;Form::INPUT_TEXT, 'options'=&gt;['placeholder'=&gt;'Enter ID...']],
          ]
      ],
      [
          'contentBefore'=&gt;'&lt;legend class="text-info"&gt;&lt;small&gt;Address&lt;/small&gt;&lt;/legend&gt;',
          'attributes'=&gt;[       // 2 column layout
              'address_1'=&gt;['type'=&gt;Form::INPUT_TEXT, 'options'=&gt;['placeholder'=&gt;'Enter telephone...']],
              'address_2'=&gt;['type'=&gt;Form::INPUT_TEXT, 'options'=&gt;['placeholder'=&gt;'Enter mobile...']],
          ]
      ],
      [
          'attributes'=&gt;[       // 2 column layout
              'town'=&gt;['type'=&gt;Form::INPUT_TEXT, 'options'=&gt;['placeholder'=&gt;'Enter telephone...']],
              'county'=&gt;['type'=&gt;Form::INPUT_TEXT, 'options'=&gt;['placeholder'=&gt;'Enter mobile...']],
              'postcode'=&gt;['type'=&gt;Form::INPUT_TEXT, 'options'=&gt;['placeholder'=&gt;'Enter ID...']],
          ]
      ],                
  ];
  if($model-&gt;formName()=='InvoiceAddress')
  {
      $rows[] = [
          'contentBefore'=&gt;'&lt;legend class="text-info"&gt;&lt;small&gt;Company Info&lt;/small&gt;&lt;/legend&gt;',
          'attributes'=&gt;[       // 2 column layout
              'vat'=&gt;['type'=&gt;Form::INPUT_TEXT, 'options'=&gt;['placeholder'=&gt;'Enter name...']],
              'company_no'=&gt;['type'=&gt;Form::INPUT_TEXT, 'options'=&gt;['placeholder'=&gt;'Enter email...']],
          ]
      ];
  }
  $rows[] = [
      'attributes'=&gt;[
          'actions'=&gt;[    // embed raw HTML content
              'type'=&gt;Form::INPUT_RAW, 
              'value'=&gt;  '&lt;div style="text-align: right; margin-top: 20px"&gt;' . 
                  Html::resetButton('Reset', ['class'=&gt;'btn btn-default']) . ' ' .
                  Html::submitButton('Submit', ['class'=&gt;'btn btn-primary']) . 
                  '&lt;/div&gt;'
          ],
      ],
  ];
  echo '&lt;p&gt;&lt;span style="color:red"&gt;&lt;strong&gt;*&lt;/strong&gt;&lt;/span&gt; denotes required field.&lt;/p&gt;';
  Pjax::begin();
  $form = ActiveForm::begin(['type'=&gt;ActiveForm::TYPE_VERTICAL, 'options' =&gt; ['id' =&gt; 'create-address-form']]);
  echo FormGrid::widget([
      'model'=&gt;$model,
      'form'=&gt;$form,
      'autoGenerateColumns'=&gt;true,
      'rows'=&gt;$rows,
  ]);
  ActiveForm::end();
  Pjax::end();
  </code></pre>
  
  <p>Javascript</p>
  
  <pre><code>$(function(){
  
      $(document).on('click', '.showModalButton', function()
      {
          if ($('#modal').data('bs.modal').isShown) 
          {
              $('#modal').find('#modalContent').load($(this).attr('value'), $("#"+$(this).attr('form')+"").serializeArray());
              document.getElementById('modalHeader').innerHTML = '&lt;h4&gt;' + $(this).attr('title') + '&lt;/h4&gt;';
          } 
          else 
          {   $('#modal').modal('show').find('#modalContent').load($(this).attr('value'), $("#"+$(this).attr('form')+"").serializeArray());
              document.getElementById('modalHeader').innerHTML = '&lt;h4&gt;' + $(this).attr('title') + '&lt;/h4&gt;';
          }
      });
  });
  </code></pre>
  
  <p>Modal</p>
  
  <pre><code>&lt;?php
  yii\bootstrap\Modal::begin([
      'headerOptions' =&gt; ['id' =&gt; 'modalHeader'],
      'id' =&gt; 'modal',
      'size' =&gt; 'modal-lg',
      //keeps from closing modal with esc key or by clicking out of the modal.
      // user must click cancel or X to close
      'clientOptions' =&gt; ['backdrop' =&gt; 'static', 'keyboard' =&gt; FALSE]
  ]);
  echo "&lt;div id='modalContent'&gt;&lt;div style=\"text-align:center\"&gt;&lt;img src=\"".Url::to('@web/images/loader.gif')."\"&gt;&lt;/div&gt;&lt;/div&gt;";
  yii\bootstrap\Modal::end();
  ?&gt;
  </code></pre>