How to do a hard redirect when in Ajaxified form? I have an Ajax multistep form and I want to redirect user to specific route when some conditions are met (specifically if the user is inactive for more than N seconds and the form is submitted after this limit).
I wanted to do the redirection in the validation handler but no luck. So I tried with a submit handler but still no luck. I'm quite buffled.
I tried to return a redirect response, I tried to set redirect response on the form state, I tried to set redirect on the form state, I have tried to disable form state rebuild, I have tried to copy the goto action logic and use an event dispatcher, I tried to unset the \Drupal\Core\Form\FormBuilderInterface::AJAX_FORM_REQUEST from request but still no luck.
I could maybe return the redirect in an Ajax callback as redirect command but I want to make this work with disabled JS so invoking window.location would not be enough.
In my case (D8), I just had to implement the RedirectCommand in my AjaxSubmit :

Code of the submit button :

$form['submit_' . $this-&gt;uniqueIdentifier] = [
  '#type' =&gt; 'submit',
  '#value' =&gt; $this-&gt;t('Submit'),
  '#attributes' =&gt; [
    'class' =&gt; ['btn', 'btn-primary']
  ],
  '#ajax' =&gt; [
    'callback' =&gt; [$this, 'submitModalFormAjax'],
    'event' =&gt; 'click',
  ],
];


Code of the submit callback:

public function submitModalFormAjax(array $form, FormStateInterface $form_state) {
  $response = new AjaxResponse();

  if ($form_state-&gt;hasAnyErrors()) {
    // Do validation stuff here
    // ex: $response-&gt;addCommand(new ReplaceCommand... on error fields
  }

  else {
    // Do submit stuff here

    $url = Url::fromRoute('page_route');
    $command = new RedirectCommand($url-&gt;toString());
    $response-&gt;addCommand($command);
  }

  return $response;
}


Do not forget to declare :

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Url;