A fix for Drupal form error messages not showing up

Submitted by jrb on Thu, 07/16/2009 - 18:07

I was having a problem with using the Login block form on the home page.  Error messages related to the login (e.g. "Password incorrect.", etc.) were not getting dispalyed when the login failed.  They would show up only after reloading the page again. 

In my template, I had this:

<div class="content">
    <?php print drupal_get_form('user_login_block'); ?>
</div>

What I figured out was that the error messages weren't actually being generated until after the call to drupal_get_form() was made.  Because this call was inside the template, they were not set for the template to display.

The fix for this is to create a function to make the call to drupal_get_form() and grab the resulting error messages (if any).  So, in a module or in template.php, you need to create a function like this:

function THEMENAME_get_login_block() {
    $form = drupal_get_form('user_login_block');
    return theme_status_messages().$form;
}

Then, in your template, do this instead:

<div class="content">
    <?php print THEMENAME_get_login_block() ;?>
</div>

This makes sure that the error messages are set before the form and the mesages themselves are displayed.

14 comments

by Ben on Fri, 08/07/2009 - 04:53

Thanks for posting - was having real issues with this!

by Mike on Tue, 09/08/2009 - 19:25

Really good tip, was having this same issue as well and never thought it might be a theme related problem

by Peter on Tue, 01/19/2010 - 20:49

Here's a message from another person you helped out a lot!

by student on Sat, 04/02/2011 - 05:47

Thanks for the tutorial...it works for me!

by ekagou on Tue, 08/23/2011 - 03:54

This is a good one.
Thanks

by satheeshkumar on Fri, 04/27/2012 - 12:27

Thanks
Its really Good

by Dilipkumar on Wed, 05/09/2012 - 04:18

Thanks its' work well

by arun on Wed, 06/06/2012 - 08:04

messages status is not showing when logout in drupal

by Arun on Wed, 06/06/2012 - 08:05

Please help me,am not able to see message status when am log out....

thanks...

by shipra on Fri, 11/16/2012 - 05:36

Thanks for posting. :-)

by V on Mon, 08/11/2014 - 12:20

Who would've guessed? Post from 2009 will solve my problem. Thanks a bunch :)

by Ward B on Wed, 10/08/2014 - 05:33

Thanks so much man, this is so very unclear in drupal!

by Suman Chalki on Mon, 11/03/2014 - 04:19

This function - theme_status_messages() did the trick.

Thanks.

by Alek on Thu, 11/08/2018 - 22:59

Odzyskiwanie danych z dysku Warszawa
http://coachfactoryoutlet.co.uk