• Development

    Quick Tip: Adding a Reset button to a Drupal form

    Using hook_form_alter or hook_form_FORM_ID_alter one can easily add a “Reset” button to a Drupal form.

    In this example we’re actually trying to reset anything the user typed after the form loaded. This will not remove the default values.

    Here’s a simple example (Drupal 6)

    It may not be appropriate for you to put your new reset element into the buttons array. Use dpm() (part of the Devel module) to show what $form looks like. If you don’t understand this, and the code above isn’t working for you, you may try $form[‘reset_button’] = array….

    Adjust the value of #weight to move the button around the form. dpm($form) will show you weights (if any) of existing elements so you can make educated decisions about the weight of your new field.

    One last note about the ‘#type’ => ‘markup’ line: this is not a requirement, but I like to include it for clarity.

    UPDATE:
    Here’s what the field might look like for Drupal 7: