Drupal form error message displaying on next page reload

  • 18 September 2013
  • mohit.aghera

Today while working I faced with very strange situation. On my theme I had displayed/rendered a block. I had displayed block on the welcome page and using following code.

In my template aka tpl file, I had written this code:

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


Now, 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. So during next page reload the error message of previous page were being displayed.

What I figured out was that the error messages weren't actually being generated untilafter 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 = render(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() ;?>

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


Courtesy: http://www.embiggen.net/post/fix-drupal-form-error-messages-not-showing


Add new comment