You are here

public function AjaxFormHelperTrait::ajaxSubmit in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php \Drupal\Core\Ajax\AjaxFormHelperTrait::ajaxSubmit()

Submit form dialog #ajax callback.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

\Drupal\Core\Ajax\AjaxResponse An AJAX response that display validation error messages or represents a successful submission.

File

core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php, line 28

Class

AjaxFormHelperTrait
Provides a helper to for submitting an AJAX form.

Namespace

Drupal\Core\Ajax

Code

public function ajaxSubmit(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->hasAnyErrors()) {
    $form['status_messages'] = [
      '#type' => 'status_messages',
      '#weight' => -1000,
    ];
    $form['#sorted'] = FALSE;
    $response = new AjaxResponse();
    $response
      ->addCommand(new ReplaceCommand('[data-drupal-selector="' . $form['#attributes']['data-drupal-selector'] . '"]', $form));
  }
  else {
    $response = $this
      ->successfulAjaxSubmit($form, $form_state);
  }
  return $response;
}