How to add a Cancel button to Drupal forms

Total
0
Shares

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

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

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

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

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

  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

Leave a Reply

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

You May Also Like