2

I have a form which I have included a reset button. Here is the code:

function submission_list($form, &$form_state) {

  ...code here...

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

  ... more code ...
}


function submission_list_reset($form, &$form_state) {

  if (isset($form_state['etd_filter'])) {
    unset($form_state['etd_filter']);
    $form_state['values']['filter_user'] = '';
    $form_state['input']['filter_user'] = '';
  }
  $form_state['rebuild'] = TRUE;
}

I thought what I have in the submission_list_reset function is correct but it still displays whatever the user typed in the filter_user text field. I have tried clearing the values and input array with no success so I must be missing something really basic here. I have searched Drupal Answers and Google but have not found an answer. The answers I have found on Drupal Answers is about adding a reset button but not about clearing specific fields.

Thanks.

2 Answers 2

2

You can attach the reset function in this way:

function submission_list($form, &$form_state){
  $form = array();
  $form['name'] = array(
    '#title' => 'Name',
    '#type' => 'textfield',
  );
  $form['surname'] = array(
    '#title' => 'Surname',
    '#type' => 'textfield',
  );
  $form['actions'] = array(
    '#type' => 'actions',
    'submit' => array(
      '#type' => 'submit',
      '#value' => t('Submit'),
      '#attributes' => array(
        'class' => array('my-submit-button-class')
      ),
      '#submit' => array('your_submit_function')
    ),
    'reset' => array(
      '#type' => 'submit',
      '#attributes' => array('class' => array('my-reset-button-class')),
      '#value' => t('Reset form'),
      '#submit' => array('submission_list_reset')
    )   
  );
  return $form;
}

function submission_list_reset($form, &$form_state) {
  $form_state['values']['name'] = '';
  $form_state['values']['surname'] = '';
}

function your_submit_function($form, &$form_state) {
  drupal_set_message('The form has been submitted');
}

But please consider to use ajax callback for this reset function, it would be better do not reloading the page only because of resetting. More info - Ajax forms in Drupal 7

1
  • In my case I am filtering the results of a database query, so I should look into using ajax to just reload that part of the page to save bandwidth. Thanks.
    – user5013
    Aug 9, 2013 at 21:28
1

Please use the right way form_set_value() in your validation function instead of $form_state['values']['surname']

Like this:

form_set_value($form['surname'], '', $form_state);

Update:

Please make sure to reference the $form_state and $form to the same variable in memory by making sure there is & sign before the parameters like this:

function your_submit_function(&$form, &$form_state) {
2
  • I used the accepted solution but used form_set_value() on validate function as you suggested. However, to make it work, I also had to add this in the submit function I had: $form_state['input'] = array(); $form_state['rebuild'] = TRUE;
    – Roger
    Feb 20, 2018 at 12:49
  • Notice I had to add that these two lines of code because my form is an AJAX multistep form.
    – Roger
    Feb 20, 2018 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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