I'm building a front page for anonymous users (via the front module) that offers them, among other things, a login block. Since this is a "full" front page (rather than themed), I have to handle everything myself, including the error messages. I've found drupal_get_messages and theme_status_messages, and they're mostly working. EXCEPT: the messages show up one page submission too late: If I click the "log in" button without entering anything into the username or password fields, I get returned to the front page with no errors. If I click the "log in" button again, perhaps after having filled out the login form incorrectly, I now get the error messages for "user name or e-mail field is required" (I'm using logintoboggan) and "password field is required". Grr.

This is arguably worse than no error messages at all; is there any way to deal with this off-by-one problem? Thanks!

Comments

jim_at_miramontes’s picture

I apologize in advance for being one of those "let me bump this back up" people, but I thought I'd try this again, and see if there are any new thoughts about this "off by one" problem. As I think about it, I've seen much the same thing happen in other settings, where the results of a drupal_set_message don't show up until the page is re-invoked. Does anyone out there have any insights into what can be done here? Thanks!

jim_at_miramontes’s picture

For future explorers who may come this way:

The page in question was a heavily customized login page, in which I was creating the login block by calling

$block = module_invoke('user', 'block', 'view', 0);

and fetching the error messages (if any) by doing

$theMessages = theme_status_messages('error');
if ($theMessages) {
   print $theMessages;
}

The page is now working properly; the problem was that I was (originally) displaying the messages before I had created $block; thus (as I now understand) the error messages hadn't been created yet. Once I created $block before fetching the messages, things began to work correctly.

I guess this counts as a "duh" moment, but whatever...

xamdam’s picture

... for posting the your problem's solution.

It was the missing hint so solve my problem, which was very close to yours.

I had to call

drupal_get_form($form);

before

theme_status_messages('error');

Jenechka’s picture

Thank you for your help. I have the same reason :)