Hi,

I have a table whith 3 submit form. The goal is to perform some code based on the clicked submit button.
The submit form is call by a (drupal_render(drupal_get_form('achats_form', $v)));
As you see I add a third argument that'll be used in the achats_form function. Drupal will add $v in $form_state that can be used in the form_submit function.

The problem is when I submit the second button ($v = 2), the dsm($form_state) called in the form_submit function returns weird results :
$form_state['buttons'][0]['#name'] = 1 instead of 2,
$form_state['triggering_element'][0]['#name'] = 1 insted of 2,
$form_state['clicked_button'][0]['#name'] = 1 insted of 2,
But,
$form_state['values'] return somewhere '2' => 'valider'
$form_state['input'] return somewhere '2' => 'valider'

In fact, whatever the button I clicked, the return value in $form_state is a mix betwen form ($v = 1) and the clicked form.
What is wrong ?

Thanks
Here is my code :


/**
 * Implements hook_menu().
 */
function achats_menu() {
  $items['test'] = array(
    'page callback' => 'achats_test',
    'title' => 'test',
    'access callback' => TRUE,
  );

  return $items;

function achats_test() {

  $nb = array(1, 2, 3);
  $row = array();
  foreach ($nb as $k => $v) {
    $row[$v] = array(drupal_render(drupal_get_form('achats_form', $v)));
  }
  $output = theme('table', array(
    'rows' => $row,
    'header' => array(),
    'sticky' => False,
    'empty' => t('pas de form'),
      )
  );
  $output .= theme('pager', array(
    'tags' => array()
      )
  );

  return $output;
}

function achats_form($form, &$form_state, $v) {

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

function achats_form_submit($form, &$form_state) {
  dsm($form_state);
 
}

}



Comments

bneel’s picture

The idea is you can't have multiple form_id on the same page, so if you want the same form to appear more than once, you need to change the 'form_id' somehow.
You can change the form_id with a hook_forms (different to hook_form).
Here I call 3 different form_id from a loop.

function achats_test() {
  $nb = array(1, 2, 3);
  $row = array();
  foreach ($nb as $k => $v) {
    $row[$v] = array(drupal_render(drupal_get_form('achats_form_' . $v, $v)));
  }
  $output = theme('table', array(
    'rows' => $row,
    'header' => array(),
    'sticky' => False,
    'empty' => t('pas de form'),
      )
  );
  $output .= theme('pager', array(
    'tags' => array()
      )
  );

  return $output;
}

Here I specify foreach form_id a callback to the form function.

function achats_forms($form_id, $args) {

  $forms = array();
  if (strpos($form_id, 'achats_form_') === 0) {
    $forms[$form_id] = array(
      'callback' => 'achats_test_form',
      'callback arguments' => array($args[0]),
    );
  }
  return $forms;
}

Here is the form.

function achats_test_form($form, &$form_state, $v) {
  dsm($v);
  $form[$v] = array(
    '#type' => 'submit',
    '#name' => $v,
    '#value' => t('Valider') . $v,
    '#submit' => array('achats_test_form_submit'),
    '#base' => 'achats_test_form',
  );

  return $form;
}

function achats_test_form_submit(&$form, $form_state) {
  dsm($form_state);
  dsm($form);
}

Know I can find in 'clicked_button' or 'trigerred_element' in $from_state, the correct clicked button.
I use this blog post to solve my problem :
http://www.computerminds.co.uk/drupal-6-multiple-instances-same-form-one...
And this : http://stackoverflow.com/questions/5997512/multiple-forms-in-drupal-7
And this : http://drupal.org/node/200106

The full code

function achats_test() {
  $nb = array(1, 2, 3);
  $row = array();
  foreach ($nb as $k => $v) {
    $row[$v] = array(drupal_render(drupal_get_form('achats_form_' . $v, $v)));
  }
  $output = theme('table', array(
    'rows' => $row,
    'header' => array(),
    'sticky' => False,
    'empty' => t('pas de form'),
      )
  );
  $output .= theme('pager', array(
    'tags' => array()
      )
  );

  return $output;
}

function achats_forms($form_id, $args) {

  $forms = array();
  if (strpos($form_id, 'achats_form_') === 0) {
    $forms[$form_id] = array(
      'callback' => 'achats_test_form',
      'callback arguments' => array($args[0]),
    );
  }
  return $forms;
}

function achats_test_form($form, &$form_state, $v) {
  dsm($v);
  $form[$v] = array(
    '#type' => 'submit',
    '#name' => $v,
    '#value' => t('Valider') . $v,
    '#submit' => array('achats_test_form_submit'),
    '#base' => 'achats_test_form',
  );

  return $form;
}

function achats_test_form_submit(&$form, $form_state) {
  dsm($form_state);
  dsm($form);
}