mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-12-23 05:29:56 +05:30
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:
parent
4265b37543
commit
51fb3afe76
@ -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();
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user