Drupal - How to add reset button to forms?

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.


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;
}

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;
}

Tags:

Forms

7