How to add a Cancel button to Drupal forms

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...

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

Leave a Reply

*

*


  • yyyfff43
    29 October 2009 at 17:41

    thank you so much

  • rsevero
    26 November 2009 at 22:57

    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’)),
    );

  • MadtownLems
    16 December 2009 at 18:23

    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!

  • Anonymous
    24 December 2009 at 22:33

    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).

  • sim6
    27 January 2010 at 15:31

    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!

  • xyz
    22 February 2010 at 09:01

    super coding,it is working fine

  • hnd
    16 May 2010 at 19:56

    it works.. thanks a lot..

  • Dmitry
    30 September 2010 at 22:38
  • loony
    29 September 2011 at 14:08

    in D7 u need to do this
    ‘button’,
    ‘#attributes’ => array(‘onClick’ => ‘location.replace(\”. $_SERVER[‘HTTP_REFERER’] .’\’); return false;’),
    ‘#value’ => t(‘Cancel’),
    );
    ?>

  • strony www
    1 October 2011 at 13:51
  • lmeurs
    6 February 2012 at 17:37

    @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

  • Giovanni
    22 February 2012 at 09:57

    Thanks man, this really helped me in a pressure moment. You must be proud! Your post from 2009 is still helping people :)

  • Devaraj
    6 December 2012 at 14:57

    Thanks .its working for me.