react-validation icon indicating copy to clipboard operation
react-validation copied to clipboard

how to reset form on submit?

Open yuvarajdks opened this issue 7 years ago • 5 comments

I'am new to react java script. Form get submitted need to reset the form field. If i make Sate value empty form-error message is displaying .

yuvarajdks avatar Sep 13 '17 06:09 yuvarajdks

I'm experiencing the same issue. Any updates or ideas on this one?

cblaettl avatar Sep 25 '17 13:09 cblaettl

Hi @cblaettl, @yuvarajdks

I resolved with this way

state = {
  errors: {},
  form: {
    question1: '',
    question2: '',
    question3: '',
    question4: '',
    question5: '',
    question6: '',
  },
};

handleOnSubmit = e => {
  e.preventDefault();
  this.setState({
      errors: this.form.validateAll(),
    },
    () => {
      const listErrors = Object.keys(this.state.errors);
      if (listErrors.length > 0) {
        /** Shows the errors, because if previously I clean to it,
         * it can't show again the errors, you have to force the showing*/
        listErrors.forEach(name => {
          setTimeout(() => {
            this.form.showError(name, 'required');
          }, 100);
        });
      } else {
        /**
         * Form validated
         */
      }
    }
  );
};

handleClearForm = e => {
  e.preventDefault();
  this.setState({
    errors: {},
    form: {
      question1: '',
      question2: '',
      question3: '',
      question4: '',
      question5: '',
      question6: '',
    },
  });
  Object.keys(this.state.form).forEach(name => {
    setTimeout(() => {
      /** clean the errors in dom*/
      this.form.hideError(name);
    }, 100);
  });
};

rafaesc avatar Sep 25 '17 19:09 rafaesc

@rafaesc If we have to handle manually like this, we had not to use this package. We expect a function to reset the form.

huuthang1993 avatar Jun 30 '18 07:06 huuthang1993

I would agree with @huuthang1993 - this should be built in functionality to reset a form. There's another issue where a DOM node is removed, but the validation still tries to validate it.... a simple reset method would fix that.

graham-saunders avatar Sep 06 '18 14:09 graham-saunders

Any solution on "where a DOM node is removed, but the validation still tries to validate it"?

LaaHudra avatar Dec 03 '18 19:12 LaaHudra