I made a custom form using FAPI (Drupal 7), with '#required' attribute in some form elements. Besides this, I use hook_form_validate() to check some business rules.

The point is at first submission, required fields appears red-bordered, but still no error message.

On second submit, the error messages appear with both required and business rules .

Is there a way, to have the second behaviour on first attempt ?

Thank you.

  • Does "no error message" mean your custom error message does not show up, or there's no error message at all?
    – BetaRide
    Commented Jun 27, 2012 at 9:45

2 Answers 2


This behaviour is usually caused by the form being rendered too late in the page build process (for example by calling echo render(drupal_get_form('form_id')); directly in a template file).

By the time the form submits, the session messages have already been prepared for rendering, and so when more messages are added by your form's validation, they don't display until the next page load.

Without knowing exactly where you're rendering/preparing the form is hard to offer advice on how to fix it. I think it would suffice to say that if you're rendering the form directly in a template file, you should move it to a preprocess function at least.

The better way would be to prepare and render the form at the module level, rather than the theme level, as that will be processed early enough in the page load for this problem to disappear.

To address your comment:

I think the problem is definitely where you're calling the form building function from. You should be able to just take that code and put in a preprocess function, preferably in a module file as that pretty much guarantees the call won't come too late:

function MYMODULE_preprocess_page(&$vars) {
  if ($_GET['q'] == 'some/path') {
    $vars['the_form'] = module_invoke('my_module', 'block_view', 0);

Then just print it if it's available in page.tpl.php:

<?php if ($the_form): echo $the_form; endif; ?>
  • Hello again Clive ! I think we are on the right track there... I used a custom module to layout out the form. I used a hook_block_view, that internally calls a drupal_get_form(myform_build_function). This block is then called within a page-something.tpl.php, like so : $block = module_invoke('my_module', 'block_view', 0); print render($block['content']); Is there something wrong with that ? The thing is I cannot render the form directly (as I did first), with a hook_menu, as the 'page' needs to be managed with node_hierarchy... Any ideas what I could do ? Thx.
    – Cypher
    Commented Jun 27, 2012 at 21:13
  • I've updated the answer, it should do the trick
    – Clive
    Commented Jun 27, 2012 at 21:29

For those interested, the solution proposed by Clive did not work any better. Thanks to him anyway !

I found out a workaround, by adding this in my *.module :

     * Implements hook_node_view
    function MY_MODULE_node_view($node, $view_mode, $langcode) {
      if($node->type =='SOME_TYPE'){
          $node->content['order_form'] = drupal_get_form('order_form');

This way, the form is output correctly to the page, without further ado, and the validation messages appears right on time.

  • Ah yeah, that would definitely work better :)
    – Clive
    Commented Jun 28, 2012 at 10:28

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.