I want to display an error message when I submit a form. I added an AJAX callback to the form, and the error message appears after I refreshed the page.
I first tried the following code, but it doesn’t work.
$response = new DrupalCoreAjaxAjaxResponse(); $message = drupal_get_messages(); $output = $response->addCommand(new DrupalCoreAjaxPrependCommand(".region-highlighted",$message)); return $output;
I also tried the following code.
$status_messages = array('#type' => 'status_messages'); $text = Drupal::service('renderer')->render($status_messages); return $text;
Sponsored by SupremePR