1

I have a form:

function payment_system_coeff_form($form, &$form_state) {
  list($_SESSION['payment_systems'], $_SESSION['coefficients']) = payment_system_table();

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  return $form;
}

function payment_system_coeff_form_submit($form, &$form_state) {
  var_dump($form_state['input']);
  exit;
}

It works, when I submit it - I get the text printed.

But when I add the theme for it:

function payment_system_theme($existing, $type, $theme, $path) {
  $path = drupal_get_path('module', 'payment_system');

  return array(
    'payment_system_coeff_form' => array(
      'render element' => 'form',
      'path' => $path,
      'template' => 'table',
    ),
  );
}

table.tpl.php

<table cellspacing="0" cellpadding="4" border="1">
  <tr>
    <th></th>
    <?php
      foreach ($_SESSION['payment_systems'] as $name) {
        print "<th>$name</th>";
      }
    ?>
  </tr>
  <?php
    $i = 0;
    foreach ($_SESSION['payment_systems'] as $index1 => $name) {
      print '<tr>';
      print "<th>" . $_SESSION['payment_systems'][$index1] . "</th>";

      $j = 0;
      foreach ($_SESSION['payment_systems'] as $index2 => $name) {
        if ($j < $i) {
          print '<td></td>';
        }
        else if ($j === $i) {
          print '<td>X</td>';
        }
        else {
          if (isset($_SESSION['coefficients'][$index1][$index2])) {
            $form["coefficients[$index1][$index2]"] = array(
              '#type' => 'textfield',
              '#required' => FALSE,
              '#size' => 10,
              '#attributes' => array(
                'name' => "coefficients[$index1][$index2]", 
                'style' => 'text-align: right;'
              ),
              '#value' => $_SESSION['coefficients'][$index1][$index2],
             );

            print "<td>" . render($form["coefficients[$index1][$index2]"]) . "</td>";
          }
          else {
            print "<td><input type='text' name='coeffitient[$index1][$index2]' value='' /></td>";
          }
        }
        ++$j;
      }

      print '</tr>';
      ++$i;
    }
  ?>
</table>
<div style="text-align: center;"><?php print render($form['submit']); ?></div>

Output of the form looks like this:

Screenshot of the form output

It stops working: I don't get any text when submitting the form. How to fix?

  • Does table.tpl.php have drupal_render_children($form); in it? – Clive Sep 4 '14 at 7:55
  • @Clive "Does table.tpl.php have rupal_render_children($form); in it?" - no. I added the code and output screenshot. – user4035 Sep 4 '14 at 7:59
  • 2
    Can you guess what I'm going to say next? ;) (if not, add drupal_render_children($form); to the bottom of the template) – Clive Sep 4 '14 at 8:06
2

I know you've found an answer to your question, but I just want to explain why did you get that problem.

Drupal adds form_build_id, form_token, form_id (hidden fields) for security reasons and it's some purposes, while you're theming the form with template file and render its fields manually usually miss to include those fields, after that you form submission stops working. Recently this problem happened in my Symfony project (I'd found the solution) and after few days in my Drupal project, the solution was the same;

If you render the fields manually and also call drupal_render_children function, you will find that fields can be duplicated, my solution was rendering those fields also, like this:

print render($form['form_build_id']);
print render($form['form_token']);
print render($form['form_id']);

In my case for some reason drupal_render_children duplicated my form fields (I think this is normal, because I manually rendered the fields plus called drupal_render_children), that's why I ended up with this solution.

1

I added print drupal_render_children($form); to the end of the template file, and it works now.

  • 2
    I didn't put this in as answer as I was sure this is a duplicate that's been answered before - couldn't find it though, thanks for putting it in – Clive Sep 4 '14 at 8:38
  • Also needs to add <form> wrapper element in your .tpl.php. – Henry Nov 3 '15 at 2:29

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.