5

I've been researching this matter for a while with no clear answer yet.
So, here are a few questions ...

  1. I'm assuming (correct me if I'm wrong) that Drupal 7 doesn't support reset buttons. It appears, according to the form API (as discussed in Pro Drupal 7 Development), that all the buttons (input type="submit", button, and image_button) are by default submit buttons. There doesn't appear to be a way to override the defaults either.

    So, if I wanted to add functionality to my forms for my users to clear forms (if they choose), would I have to create a module?

  2. Would I be able to pull this off by using the general button type with executes_submit_callback set to false and by adding some type of Ajax function to the mousedown event?

** EDIT: I added the following to my hook_form_alter() **

$form['reset'] = array(
'#input' => TRUE,
'#name' => 'reset',
'#button_type' => 'reset',
'#executes_submit_callback' => FALSE,
'#limit_validation_errors' => TRUE,
'#process' => '',
'#theme_wrappers' => array('button'),
);

I got the following error message & warning:

Notice: Undefined index: #type in _form_builder_handle_input_element() (line 1949 of /var/www/vhosts/lae/drupal-7.11/includes/form.inc).

Warning: Invalid argument supplied for foreach() in form_builder() (line 1763 of /var/www/vhosts/lae/drupal-7.11/includes/form.inc).

Is there a way to add the "reset" button type to the types of buttons without crashing the entire form.inc?

9

My preferred way to do this is.

Add the button to your form like so:

$form['options']['reset'] = array(
  '#type' => 'submit',
  '#value' => t('Reset'),
  '#submit' => array('MY_MODULE_FORM_ID_reset'),
);

Then create the reset function like so:

function MY_MODULE_FORM_ID_reset($form, &$form_state) {
  $form_state['rebuild'] = FALSE;
}

No Javascript, no markup.

6

Don't know of support in Form API for a reset button, but simple enough to add a form alter to add one.

MYMODULE_form_alter($form, $form_state, $form_id) {
  $form['resetbutton'] = array(
    '#markup' => '<button type="reset"/>',
    '#weight' => 1000
  );
  return $form;
}
  • That was an awesome answer!!!! – dqfan2012 Apr 18 '12 at 19:43
  • Do you know if there is a way to add that reset button to the actions div? – dqfan2012 Apr 18 '12 at 20:07
  • 1
    Nevermind, I figured it out: $form['actions']['reset'] = array('#markup' => '<input id="reset" type="reset" value="reset" />', '#weight' => 1000); – dqfan2012 Apr 18 '12 at 20:14
  • What is weird, however, is that after I hit SUBMIT on my form, the reset button no longer works. It works if a user has not hit the SUBMIT button. Weird – user785179 Nov 16 '12 at 0:37
  • @user785179 form reset button resets the form fields to their original values, so after submit those fields will have value="something", and that value will be restored. Reset does not blank all fields. – Duncanmoo Jul 18 at 7:49
2
function mymodule_form_alter(&$form, &$form_state, $form_id) {

switch ($form_id) { 

case 'myform':
  $form['buttons']['reset_button'] = array(
    '#type' => 'markup',
    '#value' => '<input class="form-button" value="Reset" type="reset">',
    '#weight' => 2000,
  );
  break;
 }
 return $form;
}
1

This is 100% working:

$form['reset'] = array(
    '#type' => 'markup',
    '#markup' => '<input type="reset" value="Reset All Values" class="form-submit">',
);
0

Usually I give reset hyperlink which is pointing to current page URL using Markup form type.

When ever we click this link, it will refresh the page & will reset all the form elements. Else using javascript / Jquery we can reset the form elements by providing specific CSS Class / ID to that link.

0

None of the previous answers worked in the case I've been facing (views_exposed_form with BEF and ajax enabled). I had set up the reset button from BEF, but it was working only for one of my two views displays, whatever I tried...

I purpose another approach, in case it could help.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function moduleName_form_views_exposed_form_alter(&$form, &$form_state) {

  if (isset($form_state['view']->current_display) && 'some_display' == $form_state['view']->current_display ) {

    $submit_btn_id = $form['submit']['#id'];
    if (!empty($submit_btn_id) && !empty($form['reset'])) {
      //avoid the know issue redirecting views resets to /node
      $form['#action'] = '/' . $_GET['q'];
      // Add own reset button
      $form['submit']['#suffix'] = '<input type="reset" onclick="javascript:jQuery(this.form).clearForm();jQuery(this.form).find(\'#' . $submit_btn_id . '\').trigger(\'click\');return false;">' . t('Reset') . '</input>';
      $form['submit']['#attributes']['class'][] = 'my-views-filter-submit';
      // Remove the original RESET button.
      unset($form['reset']);
    }
  }
}

About the issue redirecting views resets to /node... onclick="history.go(0); is an alternative.

This was inspired by Julian blog add ajaxified reset button for views exposed filters but I had to adapt it substantially.

-2

This is my way to add Reset button, hope you like it :D

$form['reset'] = array(
'#markup'   => '<input type="reset" class="form-submit" />',
'#title'    => 'Reset',
);
  • 2
    No - never do this. Always use the Form API. – Clive Jul 25 '13 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.