2

I have a form, with multiple (ajax) components. I have added a simple button in the middle of this form, to validate custom fields.

$form['fieldset_2']['forward'] = array(  
  '#type' => 'button',
  '#value' => t('Forward'),
  '#name' => 'btn_forward',
);

As expected, button call validation function in which I check if all values are correct.

  1. The validation messages are not rendered immediately. I have to refresh page to get the error messages. The button doesn't use ajax.

  2. I have a field type radios and form_set_error doesn't color the element with red or something.

  • Try to use client-side validation its may be helpful for you. – SKM Jun 17 '13 at 6:59
  • Where (in what function) are you calling drupal_get_form? Sounds like it's happening too late (after Drupal has already finished setting messages for the current request). – Madis Jun 17 '13 at 7:11
  • This is very specific node, so i call it in page.tpl. Is this causing troubles? – gregab Jun 17 '13 at 7:15
  • Yes that's the problem. See my answer for the solution. – Madis Jun 17 '13 at 8:05
5

The problem is that Drupal has already finished setting messages for the current request when it gets to processing your form. To get things working properly call drupal_get_form in template_preprocess_page:

function myThemeOrModulename_preprocess_page(&$variables) {
  // For node templates.
  // $variables['node']->my_form = drupal_get_form('form_name');
  // For page templates.
  $variables['my_form'] = drupal_get_form('form_name');
}

Then render the form in your template file:

<?php print render($my_form); ?>

There was another issue for which I don't know the cause of currently, but adding $variables['messages'] = theme('status_messages'); (see theme_status_messages) inside the same preprocess_page function solved it.

  • This still does not resolve the issue. I have moved my drupal_build_form in preprocess_page function. – gregab Jun 17 '13 at 8:29
  • I have tried with debug, print $messages really does get invoked later then validation function but variable $messages is empty at that time. – gregab Jun 17 '13 at 8:36
  • Weird, I had basically the same issue in the past and doing things this way solved it. Did you flush the caches and made sure the preprocess_page function is being invoked? Is the problem the exact same as before (messages appear at the next page request)? – Madis Jun 17 '13 at 8:49
  • Yes, i flushed caches, i even logged out and back in. Just in case something is in session. Yes, it is weird, because now form is being processed before page.tpl and messages should get filled. The messages appear at the next page request. – gregab Jun 17 '13 at 8:55
  • 1
    yes, i just tried that. $vars['messages'] = $vars['show_messages'] ? theme('status_messages') : ''; did the trick. – gregab Jun 17 '13 at 9:08
0

I've run into similar situations when trying to render webform blocks from within TPL files. One solution is to create a unique region for your form. Then assign a block (webform block, custom module block or create a PHP block with your drupal_get_form() code) to the new region for the specific node only. Render your new region in your page.tpl and you should be good to go.

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.