A fix for Drupal form error messages not showing up
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.
5 comments
Thanks!
Thanks for posting - was having real issues with this!
great tip
Really good tip, was having this same issue as well and never thought it might be a theme related problem
Thank you
Here's a message from another person you helped out a lot!
Thanks for the tutorial...it
Thanks for the tutorial...it works for me!
Good fix
This is a good one.
Thanks
Post new comment