Hi,

I've got a page on which I'm generating 2 forms like so:

function test_menucallback() {
  $output = drupal_get_form('test_form', 'foo');
  $output .= drupal_get_form('test_form', 'bar');
  return $output;
}

function test_form($form_id, $myvar) {
  $form['avalue'] = array('#type' => 'value', '#value' => $myvar);

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Save'
  );
  return $form;
}

function test_form_submit($form, &$form_state) {
  echo $form_state['values']['avalue'];
}

So, my form gets drawn twice, once with avalue = 'foo', and once with avalue = 'bar'. Regardless of which form I submit, however, avalue will always be 'foo' when I get to the _submit function.

Digging a bit deeper, I can change my avalue form item to be a textfield:

  $form['avalue'] = array('#type' => 'textfield',  '#value' => $myvar);

... same problem. avalue = 'foo' in the _submit function, regardless of which form is submitted.

Now, If I change the form item again, so that it uses #default_value, instead of #value like so:

  $form['avalue'] = array('#type' => 'textfield',  '#default_value' => $myvar);

.. then it's cool. avalue will = 'foo' or 'bar', depending on which form is submitted.

What's going on here?

Comments

axyjo’s picture

Priority: Normal » Critical

Got the same problem. My #type is hidden, and #default_value doesn't change the situation for me. I've got 4 submit buttons on the page (1 unique, the other three are based from the same form template). The submit button on each of the three forms submit to the first form of its kind. Putting each individual submit button together into a fieldset does not help. Firebug reveals that the HTML outputted is correct.

Escalating as this is now verified and has been unanswered for 8 days.

eaton’s picture

Status: Active » Closed (works as designed)

If you're presenting multiple forms with the same form id on the same page, and need them to contain different values and process individually, you'll need to give them *different* form_id's. hook_forms() can then be used to map all of the one-off form IDs to the shared submit/validate/etc handlers. See http://api.drupal.org/api/function/hook_forms/6 for more information. A number of modules (like Fivestar) already use this technique effectively.

pcave’s picture

I'm rendering the same form twice on a page and I've given them unique form ids and mapped them to the same set of validation and submission handlers. However when validation fails on one form, the invalid fields are highlighted on both. But it gets more interesting. One form is exposed through a block and the other is being added to a node. Submitting the form exposed through a block does not cause the invalid fields to be highlighted on the form that is exposed through the node. But when the form on the node is submitted, invalid fields are also highlighted on the form exposed through a block.

Any thoughts?