Form api does not have any Reset button

Created on 13 September 2016, over 8 years ago
Updated 29 November 2023, over 1 year ago

Reset button (as like submit button) for form api is not implemented yet to Drupal 8.x core. Its reset the all the field values of a form.

Feature request
Status

Active

Version

11.0 🔥

Component
Form 

Last updated about 4 hours ago

Created by

🇮🇳India Manav

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇦🇲Armenia murz Yerevan, Armenia

    Until this is fixed, as a workaround you can use two approaches for now:
    1. Use the form redirect like this:

      public function buildForm(array $form, FormStateInterface $form_state) {
        ...    
        $form['input_data']['options']['clear'] = [
          '#type' => 'input',
          '#value' => $this->t('Reset'),
          '#limit_validation_errors' => [],
          '#submit' => [[$this, 'resetForm']],
        ];
        return $form;
      }
      public function resetForm(array &$form, FormStateInterface $form_state): void {
        // A workaround for the issue
        // https://www.drupal.org/project/drupal/issues/2799269
        $form_state->setRedirect('<current>');
      }
    

    2. Use the browser API to reset the form:

    $form['reset'] = [
          '#type' => 'input',
          '#value' => $this->t('Reset'),
          '#limit_validation_errors' => [],
          '#attributes' => [
            'onclick' => 'this.form.reset(); return false;',
          ];
        );
    

    But the proper API from Drupal Core would be much better, so keeping the issue as active.

Production build 0.71.5 2024