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 \Drupal\Core\Ajax\AjaxResponse();
$message = drupal_get_messages();
$output = $response->addCommand(new \Drupal\Core\Ajax\PrependCommand(".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;