react-validation
react-validation copied to clipboard
how to reset form on submit?
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 .
I'm experiencing the same issue. Any updates or ideas on this one?
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 If we have to handle manually like this, we had not to use this package. We expect a function to reset the form.
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.
Any solution on "where a DOM node is removed, but the validation still tries to validate it"?