I have a Drupal 7 form which I'm allowing the user to submit via AJAX. If the user does not fill out required fields, or enters invalid information, I can return that and alert the user via the form_get_errors() function. The problem is that these values are still on the "display stack" (not the correct term, I'm sure) - when the user refreshes the page, or navigates to another one, these previous errors are displayed.

Is there a way to set these errors not to display once I've grabbed them with form_get_errors()?

EDIT: The session variable 'messages' is set at $_SESSION['messages']['error'], which is where the display of the errors is driven from. I could wipe this out by doing something like unset($_SESSION['messages']['error']), but I feel like this is very hacky. The drupal_static_reset() function also will not help, since it doesn't eliminate the values in the messages array.


If you know the message was the last one set, a simple hack is to just:


Just be aware this only removes the message, your form will still be in error state.

  • This would totally work, but I'll hang out a bit to see if there's a suggestion that's more inline with the Drupal workflow. I just can't believe that I'm the first person to try to use Drupal in this way. I mean...submitting forms via AJAX is pretty common, right? – rybosome Dec 2 '11 at 21:48

Yes its pretty common.. :)

But I have used the same technique, that is to unset the $_SESSION array.

Some of the modules store the errors as $_SESSION['module_name]['messages']['error'], which is good so that you dont unset some else's message.

I have used AJAX for form submit many times, but still haven't run into a better or more Drupal way to do this.

Will be glad to find something better than unset or array_pop ;)


I believe you can grab all the themed error messages by theme('status_messages', array('error')) which in turn calls drupal_get_messages() which unsets the messages that got displayed. Or if you want a structured array of messages you can call drupal_get_messages('error') straight away. I'm not 100% certain this works for form errors, but I hope this helps.

Edit: Sorry didn't notice this question was from December 2011 -.-


This will help you.

drupal_get_messages($type = NULL, $clear_queue = TRUE);

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.