Sometimes we need to add a Cancel button to some of the forms we are working with in Drupal. Maybe you want the user to be redirected to another page if he presses cancel or you want to perform some other action.
This can be a very easy task if you know how to do it. But if you don’t know how to do it, i will show you in this article.
So first we need to alter the form and add the button to the form array:
$form[‘cancel’] = array(
‘#type’ => ‘button’,
‘#value’ => t(‘Cancel’),
‘#weight’ => 20,
‘#executes_submit_callback’ => TRUE,
‘#submit’ => array(‘mymodule_form_cancel’),
);
Now let’s explain. The cancel button definition has to be somewhat similar to the submit button definition but with a few differences. For example the ‘#executes_submit_callback‘ property will result in the form being submitted when the user click on the Cancel button (By the way this property defaults to false for button type and to true for submit type form elements.). But with a difference. Now we can tell with the ‘#submit‘ property what function to be executed on the submit.
So you define another function in your module (the one specified in the submit property)
function mymodule_form_cancel(){
drupal_goto(‘destinationpage’);
}
This function will be executed when the user clicked on the Cancel button. Isn’t this simple ?
13 comments
thank you so much
Answering your last question: not exactly.
This solution presents a problem if you have any “required” fields and any of them is empty when the user tries to cancel. You got a “required field” error message and the cancelation fails.
Better to define the “Cancel” button as a link to the page you want to take your users on cancelation. You can do it defining a “#markup’ type element in the form:
$form[‘cancel’] = array(
‘#value’ => sprintf(“%s“, URL_TO_GO, t(‘Cancel’)),
);
This is a good start, but doesn’t this get run AFTER any validation, such as Required fields?
I tried it out on a form with a required field, and that’s what kept happening. Pushing cancel would take me back to the same page telling me the title was required. But if i entered a title, and then pushed cancel, it would work.
Thoughts?
and thanks!
Thanks for this one!
IMO, a button is a much better solution than the “text link” version. Users (psychologically?) “feel safer” clicking a button to cancel.
From what I could tell, the validation function was OK because $form_state[‘values’] still had the original content (which was valid). While $form_state[‘clicked_button’][‘#post’] had the current (potentially invalid) data. Though I’m a newbie and may be interpreting it incorrectly.
Thanks again, it worked fine for me (Drupal 6.15).
Hi,
The next code uses javascript:
// hook_form()
function myform_form($form_state, $value) {
$form[‘cancel’] = array(
‘#type’ => ‘button’,
‘#attributes’ => array(‘onClick’ => ‘location.replace(“‘. referer_uri() .'”); return false;’),
‘#value’ => t(‘Cancel’),
);
}
Enjoy it!
super coding,it is working fine
it works.. thanks a lot..
Sometimes you can’t just redirect user using link. For example when you need to perform some actions on cancel. Here is an example how to really cancel Drupal “required field” validation http://devengineering.com/best-practices/drupal/cancellation-required-validation-drupal-6
in D7 u need to do this
‘button’,
‘#attributes’ => array(‘onClick’ => ‘location.replace(\”. $_SERVER[‘HTTP_REFERER’] .’\’); return false;’),
‘#value’ => t(‘Cancel’),
);
?>
@rsevero
You can’t find example with disabled validation here
http://devengineering.com/best-practices/drupal/cancellation-required-validation-drupal-6
@MadtownLems: add “‘#limit_validation_errors’ => array(),” to the cancel button to skip all validation. Ie.:
$form[‘cancel’] = array(
‘#type’ => ‘submit’,
‘#name’ => ‘cancel’,
‘#value’ => ‘Cancel’,
‘#limit_validation_errors’ => array(),
);
Cheers
LMeurs, Rotterdam
Thanks man, this really helped me in a pressure moment. You must be proud! Your post from 2009 is still helping people :)
Thanks .its working for me.