I have a webform created in Drupal 8 using the webform module I have to submit the data to 2 different 3rd party API.

The scenario is I have to submit the data to the 1st API if there is no error response found then submit the data to 2nd API so now I have created a submit handler and attached the handler to the webform. Now the issue is when I got an error response message while submitting the data to 1st API and showing the message using drupal_set_message() function everything is working fine but the form validation js not loading at this point.

The used code is like the below:

drupal_set_message(t($msg), 'error');

can anybody help me with this?


For form validation error messages you do not use the drupal_set_message() which by the way, it's deprecated.

Deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::addMessage() instead.

Back to my point, in forms you don't use the Drupal messager, instead you use the FormState::setErrorByName

$form_state->setErrorByName('field_name', t('Error Message'));

you can put the validation messages above the webform by -

1.put the generic element above the form

  '#type': status_messages

2.make sure the webform's status messages purge all messages using the below hook in a CUSTOM_MODULE

function CUSTOM_MODULE_webform_element_alter(array &$element, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {
  if (isset($element['#type']) && $element['#type'] === 'status_messages') {
    $element['#pre_render'][] = 'CUSTOM_MODULE_webform_element_status_messages_pre_render';

 * Pre render callback to display status messages element.
function CUSTOM_MODULE_webform_element_status_messages_pre_render($element) {
  // Replace the status messages placeholder with the rendered messages.
  // Calling drupal_get_messages(NULL, TRUE) will purge all messages, which 
  // prevent the messages block from being displayed.
  // @see \Drupal\Core\Render\Element\StatusMessages
  // @see drupal_get_messages()
  $element = [
    '#theme' => 'status_messages',
    '#message_list' => drupal_get_messages(NULL, TRUE),
    '#status_headings' => [
      'status' => t('Status message'),
      'error' => t('Error message'),
      'warning' => t('Warning message'),
  return $element;

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.