By chaldar on
In a form, if user is anonymous user ($user->uid == 0), I use drupal_set_message('my error message', 'error') to set an error message and the form displays a field with a text to encourage registration. If the user is not anonymous, the form displays some other fields.
When an anonymous user visits the page, sees the error message, logs in, and becomes authenticated user, the form display changes correctly, but the drupal error message I set earlier seems to persist. When I ask the browser to reload the page or clear the cache, the error message goes away. Which function do I need to call in my form to clear the error message automatically?
Comments
How smart do you need to be?
There's a quick answer and a slightly longer answer depending on how smart you need to be.
The quick way is to just clear all the messages from the stack:
drupal_get_messages();The next way is to clear all the error messages, which would save any information messages that may be on the stack:
drupal_get_message('error');The smartest way (assuming you know the error you want to clear) is to keep any other error messages that may have been raised by other parts of the system:
This actually clears all the errors from the message stack, then re-raises any which may have been raised outside your code.
Thanks!
Excellent! I needed to be as smart as possible and what you described worked perfectly.
Oops - code typo!
The line to clear all the error messages (leaving information messages) should actually be
Thanks again.
Noticed that. Thanks again. Found use for this idiom in several other parts of my code as well :-).
Great solution
@Mark B Thanks a million, you saved my life ;)
Drupal showing this error.. Can anyone help plz
Error
The website encountered an unexpected error. Please try again later.
please help on drupal error
PDOException: SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "10.7.16.41" and accepting TCP/IP connections on port 5432? in lock_may_be_available() (line 167 of /var/www/html/bihardemo/includes/lock.inc).