Multiple Submit Buttons

Whilst creating a custom form, for a custom module that uses a non node content type, I came across an issue with multiple submit buttons.

Whilst Drupal does document using multiple submit buttons here:

http://api.drupal.org/api/drupal/modules–node–node.pages.inc/function/node_form/7

For all the tea in china I couldn’t get this to work.

The form API seemed to pick up the first submit button and work with that.

print ‘<pre>’;
print_r($form_state);
print ‘</pre>’;

An array inside $form_state ['clicked_button'] showed that the clicked button was the first submit button in the form, not the button that was clicked.

$form_state ['values'] again shows the first submit button

Although $form_state['input'] did show that button that was clicked.

The answer in the end was very simple

All your submit buttons must share the same ‘#name’ value :

$form['another'] = array(
‘#type’ => ‘submit’,
‘#value’ => ‘Save and add another’,
‘#name’ => ‘attribute_save’,
‘#weight’ => 5,
‘#submit’ => array(‘product_another_submit’),
);
$form['return'] = array(
‘#type’ => ‘submit’,
‘#value’ => ‘Save and return’,
‘#name’ => ‘attribute_save’,
‘#weight’ => 10,
‘#submit’ => array(‘product_return_submit’),
);
$form['cancel'] = array(
‘#type’ => ‘submit’,
‘#value’ => ‘Cancel’,
‘#name’ => ‘attribute_save’,
‘#weight’ => 15,
‘#submit’ => array(‘product_cancel_submit’),
);

 

Hope that helps anyone else stuck with the same problem

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

10 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>