1

Hi gurus and fellows,

I have to generate an application form, that needs to be displayed both a as "page" (hook_menu) and as "block".

I factorized the form generation within the same function (generate_form). It works fine, except for the validation process. Clearly the validation happens (form is not submitted when it fails), but the form_set_error() is never displayed in block mode (in page mode, it works fine).

This "lack of display" also happens for regular validation ('#required'=>'true'), so I don't think my custom validation code is at stake :

  • When in page mode, the required fields get highlighted, the error message is displayed.

  • When in block mode, the required fields get highlighted (red-bordered), BUT the error message is NOT displayed.

Of course, I checked that the page.tpl.php, used to display the block, has a 'message' section, within it. Proof is my drupal_set_message() gets displayed on submit.

Oddly enough, when I put a drupal_set_message() within my custom validation function, it gets displayed, as long as (and only when) the validation passes ! Wierd...

I pasted the *.module code here : http://pastebin.com/cnwzp3UU

Any idea ?

2 Answers 2

0

Taking a bit of a stab in the dark here, but I'm suspicious of this comment in your linked code right above hook_block_view():

// used in node--landing-page.tpl.php

That suggests that you're calling this block in directly from a template file, not through the normal block system, which won't work properly.

The problem is that the template files are built too late in the page build, in this case after the system messages have already been taken from the session and cleared out for the next page. So the validation messages do get added to the messages array in the session, but they won't be outputted until the next page is loaded.

I would suggest instead implementing hook_node_view() and adding your block into the content there. That hook is run early enough on in the page build that you won't see the delay with error messages.

0

Here is how I got it to work:

--- in my custom module ---

/**
 * Implements hook_node_view( ).
 */
function my_module_node_view ( $node, $view_mode, $langcode ) {

    switch ( $node->type ) {

        case 'my_content_type':
            my_module_add_form_to_node( $node, $view_mode, $langcode );
            break;
    }
}


/**
 * This function adds the form to a $node for use in an template.
 */
function my_module_add_form_to_node ( $node, $view_mode, $langcode ) {

    $node->content['my_form']  =  drupal_get_form( 'my_form' );
}

--- In the node's template file ---

<?php print render( $content['my_form'] ); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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