3 namespace Drupal\Core\Ajax;
5 use Drupal\Core\Form\FormStateInterface;
8 * Provides a helper to for submitting an AJAX form.
12 trait AjaxFormHelperTrait {
17 * Submit form dialog #ajax callback.
20 * An associative array containing the structure of the form.
21 * @param \Drupal\Core\Form\FormStateInterface $form_state
22 * The current state of the form.
24 * @return \Drupal\Core\Ajax\AjaxResponse
25 * An AJAX response that display validation error messages or represents a
26 * successful submission.
28 public function ajaxSubmit(array &$form, FormStateInterface $form_state) {
29 if ($form_state->hasAnyErrors()) {
30 $form['status_messages'] = [
31 '#type' => 'status_messages',
34 $response = new AjaxResponse();
35 $response->addCommand(new ReplaceCommand('[data-drupal-selector="' . $form['#attributes']['data-drupal-selector'] . '"]', $form));
38 $response = $this->successfulAjaxSubmit($form, $form_state);
44 * Allows the form to respond to a successful AJAX submission.
47 * An associative array containing the structure of the form.
48 * @param \Drupal\Core\Form\FormStateInterface $form_state
49 * The current state of the form.
51 * @return \Drupal\Core\Ajax\AjaxResponse
54 abstract protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state);