Hello--
I am working on a custom homepage template (page-front.tpl.php) and I have a "sign-up for email updates" from in the right sidebar. This is a block element that is generated from a custom module. It is a very simple form, just an e-mail input and a submit button. The way the module is written, if the e-mail doesn't validate it uses the API function form_set_error to report to the user that there was a problem. If this block is placed on any page other than the homepage it works flawlessly. On the homepage it only sets the text in the field to red but does not display the error message. I have tried just including the form (rather than the entire block element) but the same problem persists. The field turns red but the error message doesn't display. Strangely, if I submit the form a second time (before the page can finish rendering) the error message appears. Also, if a valid e-mail is presented and the form goes through successfully, the success message *does* display. I also tried taking a template that *does* work and copied it and renamed it to page-front.tpl.php and then it stopped working, so it has something to do with the homepage.
In researching this online I've found that some people had this problem because the 0 user was missing. Mine isn't. Others were fixed by not including the block element as a whole but just the form. That didn't fix mine. Others could see the message in the session. I don't.
Another curiosity: If I retrieve the messages by calling drupal_set_message() and var_dump that I get different responses, depending on where it is in the code. If I do it before the form it is NULL. If I do it after the form it has the error message in it.
OK. Relevant snips of code:
MODULE:
function memberlist_join_updates_form()
{
$form['join_updates_email'] = array(
'#type' => 'textfield',
'#size' => 35,
'#default_value' => t('Enter e-mail address'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('')
);
return $form;
}
function memberlist_join_updates_form_validate($form_id, $form_values)
{
// Make sure the user entered a valid e-mail address
$address = $form_values['join_updates_email'];
if ((user_validate_mail($address)) && ($address != ''))
{
form_set_error('join_updates_email', t('Oops! Please enter a valid e-mail address.'));
}
}
TEMPLATE:
if ($messages)
{
echo '<div class="messages">'.$messages.'</div>';
}
echo drupal_get_form('memberlist_join_updates_form');
Comments
No use of drupal_get_messages?
Are you 100% certain that you are not calling drupal_get_messages() anywhere in your module or theme, or in a contributed module?
I'm wondering if, in the course of troubleshooting, you are calling the function somewhere and forgot to remove/comment it out. Since the messages queue is cleared once the function is called (unless the optional argument is changed to false) the message would be missing.
BTW +1 for the well written forum post. If only everyone detailed their questions so well =)
---------------------------------
Steven Wright
Slalom
Nope. No use of drupal_get_messages
Thanks for the suggestion. I did a grep across the entire drupal installation for "drupal_get_messages" and the only results were in the root level includes folder. None in any modules or themes. So this is still an existing problem.
As for the +1: Ha! Thanks. I think it is because 1) I want an answer and so I know more information helps, because 2) I've been on both sides of forums like this before. Drupal is just *very* new to me.
Weird and Inexplicable Solution
Part of this edit is to pop a lightbox if the e-mail address *does* validate. Me adding that lightbox to the page made it start reliably reporting the error when the form doesn't validate.
Bizarre.
A solution that worked for me
Hi,
I had a similar problem with the output messages of a custom block not being output until the next page load. There were two problems:
1) The block was not being invoked until after the message variable was output.
2) The message template variable was not being updated by my theme preprocess function.
To solve the problem, I moved the module_invoke that created the block to the theme preprocess function (or at least above where you output the message variable in your template file.) I then refreshed the message variable that was being output to the template file. See below and ask me if you have any questions.
Thank You!!!
This is exactly what i needed, thanks so much for posting your solution, i had exactly the same problem, just called theme_status_messages() on my template, now i dont have to submit twice for the messages to update.
A solution that worked for me for displaying error messages
hi,
I have front-page.tpl file, where i am calling the login_form form by using drupal_get_form('login_form'),
The error message was not getting populated, so i have added the below code in my page-front.tpl.php.
and it started displaying the error messages.
there may be other thing in your template file that can cause the message variable not to be getting populated is the theme_preprocess_page().
so check that as well.
thanks
not work in my page tpl....
i had exactly the same problem.
Tried, but not work in my page tpl..
how to solve this issue
www.thirstysix.com
I too had that issue
I needed to code a custom look for errors and messages, so naturally used
And on my template file (just a header.php include) I was going through the array and separating the types of messages to display them in different styles, that worked but there was one page refresh delay.
What I mean is that I had to submit the form incorrectly twice to get the error message, and to get ride of the error I had to refresh the page twice. After reading the comments here and how calling a block or a form from within your template can cause these issues, I noticed I was calling
drupal_get_form('user_register_form');
from within page--user.tpl.phponce I removed that line, everything is in order, the errors show up after the first form submission, and refreshing the page would get ride of the error messages.
So thank you all for the information.
Regards,
Sinan