Clearing Drupal 7 error message

asked Jan 22, 2013 in Website Builder by DruNew
Hello there

I am using Drupal 7 and in my form, if the user is anonymous, an error message is set and the forms displays text encouraging the registration. If the user is registered, the form displays other fields. My problem is that when an a anonymous user visits the site, gets the error message, and then registers, the form displays the changes correctly, but the Drupal error message that was set earlier persists. When the user reloads the page or clears the cache the error message disappear. But how can I clear this error message without having the user refresh or clear cache? Can anyone please help
Thanks in advanced

2 Answers

0 like 0 dislike
answered Jan 22, 2013 by RalphK
You can simply clear all the messages from the stack using
You can also clear all error messages, that save any information messages which may be on the stack like this
0 like 0 dislike
answered Jan 22, 2013 by randy999
A better solution would be clearing only this particular error message and keeping any other error messages. You can do this using the following code:

$error1 = drupal_get_messages('error');
foreach($error1['error'] as $error) {
  if($error != 'your custom error message') {
    drupal_set_message($error, 'error');

This will clears all the errors from the message stack, then  it will re-raises any error that  have been raised outside the code.