Reputation: 20184
I have two submit buttons on my form. One is the normal submit button that calls `my_form_submit` like-ah-so:
$form['fieldset']['submit'] = array(
'#type' => 'submit',
'#value' => 'Show Applications',
);
I want to add another button that will submit the form, but call a different submit handler. Is there any way to do this? I have looked at adding the #submit
property to an element like-ah-so:
$form['fieldset']['evil_plan'] = array(
'#type' => 'submit',
'#value' => 'Big Red Button',
'#submit' => array('steal_formula_for_icantbeliveitsnotbutter'),
);
hoping to call the `steal_formula_for_icantbeliveitsnotbutter` function but no worky. When I click the Big Red Button the normal `my_form_submit` function is called, which sets back my plan to make millions off of a butter substitute. Who can shed some light on this dark mess?
Upvotes: 3
Views: 2945
Reputation: 59947
for Drupal 5, this is the only solution:
function my_form_submit($form_id, $form_values) {
if ($form_values['op'] == 'Show Applications') {
// handle 'Show Applications' submission
}
elseif ($form_values['op'] == 'Big Red Button') {
// handle 'Big Red Button' submission
}
}
same for my_form_validate
.
in Drupal 6, this can easier be done with the newly introduced custom #submit and #validate handlers for form buttons.
Upvotes: 4
Reputation: 19441
Edit: A closer check of the Forms API Documentation revealed that my initial answer below is valid for Drupal 6, but only partially valid for Drupal 5. Comparing the Drupal 5 docs with the Drupal 6 docs shows that in Drupal 5, you can only register callback functions on the form itself - the '#submit' property of a button is just a boolean indicating that the button is to be handled as a submit button. So adding 'per button' callbacks is a Drupal 6 only feature!
For the given Question, this means that there is only the if/elseif option suggested by ax, or the variation of registering two submit callbacks for the form, each checking if they are called for the right button (basically the if/elseif version, but using two separate callbacks).
(initial answer)
There are several mechanisms at work here:
$form['#submit']
). The ones registered for the form get called for every submit button, the ones registered for an individual button only for that one. (NOTE: Important Drupal 5/6 difference here, see edit below.)'#submit'
and '#validate'
arrays.So in your case, you could do the if/elseif switch suggested by ax, or you'd need to unset the 'global' callbacks for the form, moving it explicitly to the default submit button:
$form['fieldset']['submit']['#submit'] = $form['#submit'];
unset($form['#submit'];
(same thing for validate callbacks)
After that, your posted example for the Big Red Button executing the evil plan should work as expected ;)
Upvotes: 3