0

I have a custom form which I render in a custom template. I render the elements separately but the submit button does not fire the data to the validation/submit function. When I render the whole form at once it works fine.

This does not work:

$form = drupal_get_form('my_module_form');
print render($form['title'];
print render($form['submit'];
print drupal_render_children($form);

This works:

$form = drupal_get_form('my_module_form');
print render($form);

This also works:

$form = drupal_get_form('my_module_form');
print render($form['title']);
print render($form);

So apparently I'm missing something but can't find what. Can somebody give me a hint what I'm doing wrong?

2

Check the html your template outputs - I am pretty sure it is missing the <form> tag around your fields - without that element your form wont submit however as it contains necessary data (post methode and action to name two).

drupal_get_form() retrieves the entire render array for a functional form, but you only pick two form elements to print to screen. It does display the elements because they are themselves valid render arrays, but the functionality of the form is lost that way.

If you really want to ommit fields in the template try to unset the fields you do not want to display. E.g. unset($form['field_to_hide']). This leave the render array of the form intact.


That said, hidding fields is commonly done in a preprocess function and not in the template itself. Since it is a custom form you can do it directly in your form declaration.

$form['field_to_hide']['#access'] = 0;
$form['field_to_hide']['und']['#disabled'] = TRUE;

You can use this approach to set custom data you want to save with the form and the user isn't supposed to see. It's basically a hidden field.


If you just want to seperate the fields to add additional wrapper elements or classes you can do that in the form as well:

$form['your_field'] = array(
  //[...] 
  '#prefix' => '<div class="some-wrapper-class">',
  '#suffix' => '</div>',
  '#attributes ' => array('class' => array('element-class'));
);

Check the Form API page to see for which element the two options are available for: FAPI

|improve this answer|||||
  • Indeed it was the missing <form> tag, stupid me. Code below does the trick <form> print render($form['title']; print render($form['submit']; print drupal_render_children($form); </form> – FeBe Jul 20 '16 at 11:32
0

Please try to call "print drupal_render_children($form);" at the bottom of your template file. It may help.

|improve this answer|||||
  • Thanks for your response. Already did that (see code in original post) – FeBe Jul 20 '16 at 11:32

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.