10

I have a page with a very simple template:

<!DOCTYPE html>
<html>
<head><title>TEST REGISTRATION PAGE</title></head>
<body>
    <?php 
        print drupal_get_form('user_register');
        print $messages;
        print $closure;
        print $main_content;
    ?>
</body></html>

Basically, it displays the registration form and nothing else.

  1. I enter some bad data on the form — an already used username (admin), and no email address.
  2. I click "create new account".
  3. The page seems to submit and reload, but nothing has happened — no error messages are displaying.
  4. I refresh the page (F5).
  5. Firefox displays "to display this page, Firefox must send information that will repeat any action that was performed earlier" — so I know that my POST went through previously.
  6. I click "resend" in that dialog window.
  7. My error messages appear — "e-mail address field is required", "the name admin is already taken".

This occurs for all of the forms I've tested so far, not just the registration form.

Why is this happening? What can I do to fix it so that the error messages display the first time?

I'm happy to do whatever I need to do debug this, but I need some direction to start looking in. :)

4

Most likely the problem is that you are calling the form when the messages already have been created, which is why they don't appear. You need to call the form, earlier in the process of the page being built or get the messages, with drupal_get_messages.

What you really should do it is to create this functionality in a module and theme the page instead, of doing this in your theme. You could potentially run into many more problems like this.

| improve this answer | |
  • OK, I think I finally might have fixed this. I've written a module called 'my_register_form' that does nothing but return drupal_get_form('user_register') in the $block['content']. I include that module via context and then the registration error messages show up correctly after submission. It seems overly complicated but it works and it lets me include the login / register forms how I want them. Thanks heaps for your help. – George Aug 2 '11 at 8:01
3

We solved the form validation problem, (form validation happening after the messages have been rendered, and showing up on the next request), by..

forcing the form validation in hook_node_view

/**
 * This is a fix for a bug where the validation error messages lag a 
 * full request behind form submissions
 */
function your_module_node_view($node, $view_mode){
  // check that form id is the form you want
  if ( !isset( $_POST['form_id'] ) || $_POST['form_id'] != 'your_form_id' ){
    return;
  }
  // this forces validation of the form 
  drupal_get_form('your_form_id');
}
| improve this answer | |
2

I also suffered from this problem. @googletorp's answer helped me to solve it.

At the beginning of page tpl. Get the rendered content of forms i.e. for my case:

$register_form = drupal_get_form('user_register_form');
$register_form_content = render($register_form);

$form_user_pass = drupal_get_form('user_pass');
$form_user_pass_content = drupal_render($form_user_pass);

$login_form = drupal_get_form('user_login_block');
$login_form_content = render($login_form);

Then at the message print section, get the messages calling drupal_get_messages() at the page tpl. $messages is still unable to fetch the messages without another page refresh.

<?php 
    $messages = drupal_get_messages('error');
    if(is_array($messages['error']) && count($messages['error'])) {
?>
    <div id="messages">
        <div class="section clearfix">
            <div class="messages error">
                <ul>
                    <?php 
                    foreach($messages['error'] as $item) {
                        echo '<li>'.$item.'</li>';
                    }
                    ?>
                </ul>
            </div>
        </div>
    </div>
<?php
    }
?>

Then print the rendered form content at the proper section and it solved :).

| improve this answer | |
1

Not directly a solution for the question asked, but I came across this when I was having the same problem:

If your form block is called directly from the tpl.php file (perhaps for layout reasons), then you'll get delayed messages.

I.e. I was loading a block with the following code in my template file:

$vars['contact_details'] = module_invoke('viewusercontact', 'block_view', 0);

And then rendering it in my tpl.php file.

<?php print render $contact_details ?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.