AJAX form submit error when sending form to twig in hook_preprocess_page I have a simple AJAX form that I can't submit when the form is passed from module to the template in hook_preprocess_page.  The same form works fine if sent from controller, it's just that the layout is complicated and I prefer to write the HTML part in the twig file and integrate the form where it fits best.

The form class:

    &lt;?php

namespace Drupal\mymodule\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class SubmissionForm extends FormBase {
  public function getFormId() {
    return 'mymodule_submission_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['#prefix'] = '&lt;div id="submission-form-wrapper"&gt;';
    $form['#suffix'] = '&lt;/div&gt;';

    $form['mail'] = array(
      '#title' =&gt; 'E-mail',
      '#type' =&gt; 'textfield',
      '#required' =&gt; TRUE,
    );

    $form['actions'] = array(
      '#type' =&gt; 'actions'
    );

    $form['actions']['submit'] = array(
      '#type' =&gt; 'submit',
      '#value' =&gt; 'Submit',
      '#attributes' =&gt; array(
        'class' =&gt; array(
          'btn',
          'btn-success',
          'use-ajax-submit'
        ),
      ),
      '#ajax' =&gt; array(
        'wrapper' =&gt; 'submission-form-wrapper',
      )
    );

    return $form;
  }

  public function validateForm(array &amp;$form, FormStateInterface $form_state) {
  }

  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
  }
}


The preprocess function:

/**
 * Implements hook_preprocess_HOOK().
 */
function mymodule_preprocess_page(&amp;$vars) {
  if (\Drupal::service('path.matcher')-&gt;isFrontPage()) {    
    $vars['page']['submission_form'] = \Drupal::formBuilder()-&gt;getForm('Drupal\mymodule\Form\SubmissionForm');
  }
}


The error:


  TypeError: Argument 1 passed to
  Drupal\Core\Ajax\AjaxResponse::setAttachments() must be of the type
  array, null given, called in
  /Volumes/Work/Git/myproject/public_html/core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php
  on line 56 in Drupal\Core\Ajax\AjaxResponse->setAttachments() (line 37
  of
  /Volumes/Work/Git/myproject/public_html/core/lib/Drupal/Core/Render/AttachmentsTrait.php).



it's a fresh Drupal 8.7.10
devel and kint as contrib modules enabled
core inline form errors enabled
the form has only one required textfield and the submit button


Any suggestions on how to fix this?

Thank you!
The #ajax['wrapper'] replaced with #ajax['wrapper_selector'] that's why the form disappear see this issue:

Try with:

&lt;?php

namespace Drupal\mymodule\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class SubmissionForm extends FormBase {

  public function getFormId() {
    return 'mymodule_submission_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['#prefix'] = '&lt;div id="submission-form-wrapper"&gt;';
    $form['#suffix'] = '&lt;/div&gt;';

    $form['mail'] = [
      '#title'    =&gt; 'E-mail',
      '#type'     =&gt; 'textfield',
      '#required' =&gt; TRUE,
    ];

    $form['actions'] = [
      '#type' =&gt; 'actions',
    ];

    $form['actions']['submit'] = [
      '#type'       =&gt; 'submit',
      '#value'      =&gt; 'Submit',
      '#attributes' =&gt; [
        'class' =&gt; [
          'btn',
          'btn-success',
          'use-ajax-submit',
        ],
      ],
      '#ajax'       =&gt; [
        // Change here the wrapper with wrapper_selector
        // 'wrapper' =&gt; 'submission-form',
        'wrapper_selector' =&gt; '#submission-form-wrapper',
      ],
    ];
    return $form;
  }

  public function validateForm(array &amp;$form, FormStateInterface $form_state) {
  }

  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
  }
}