Clear form errors before submitting (#30)

* Clear form errors before submitting

* Move error clearing logic to FormModel

* Revert setError signature to the previous state

* Expose clearErrors proxy method on Form like setErrors
This commit is contained in:
kotwys 2020-08-06 00:27:22 +04:00 committed by GitHub
parent 4265b37543
commit 51fb3afe76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -131,6 +131,7 @@ export default class Form extends React.Component<Props, State> {
}
if (form.checkValidity()) {
this.clearErrors();
let result: Promise<void> | void;
if (hasForm(this.props)) {
@ -187,6 +188,8 @@ export default class Form extends React.Component<Props, State> {
this.props.onInvalid(errors);
}
clearErrors = () => hasForm(this.props) && this.props.form.clearErrors();
onFormSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

View File

@ -139,6 +139,14 @@ export default class FormModel {
});
}
/**
* Clear errors in form fields
*/
clearErrors(): void {
this.errors = {};
Object.values(this.fields).forEach((field) => field.setError(null));
}
getFirstError(): ValidationError | null {
const [error] = Object.values(this.errors);