|
1 | 1 | const Validator = require("validatorjs");
|
2 | 2 | import { IReactComponent, IOptions, IValidatorErrors, IDynamicKeyValues, ReactFormSubmitEventHandler,
|
3 |
| - ReactFormInputValidation as BaseValidation } from "./specs/react-form-input-validator.spec"; |
| 3 | + ReactFormInputValidation as BaseValidation, Lang } from "./specs/react-form-input-validator.spec"; |
4 | 4 |
|
5 | 5 | class ReactFormInputValidation extends BaseValidation {
|
6 | 6 | private component: IReactComponent;
|
@@ -29,15 +29,15 @@ class ReactFormInputValidation extends BaseValidation {
|
29 | 29 | Validator.registerAsync(name, callbackFn);
|
30 | 30 | }
|
31 | 31 |
|
32 |
| - static setMessages(name: string, values: object): void { |
33 |
| - Validator.setMessages(name, values); |
| 32 | + static setMessages(langCode: Lang, values: object): void { |
| 33 | + Validator.setMessages(langCode, values); |
34 | 34 | }
|
35 | 35 |
|
36 |
| - static getMessages(name: string): object { |
37 |
| - return Validator.getMessages(name); |
| 36 | + static getMessages(langCode: Lang): object { |
| 37 | + return Validator.getMessages(langCode); |
38 | 38 | }
|
39 | 39 |
|
40 |
| - static getDefaultLang(): string { |
| 40 | + static getDefaultLang(): Lang { |
41 | 41 | return Validator.getDefaultLang();
|
42 | 42 | }
|
43 | 43 |
|
@@ -95,40 +95,52 @@ class ReactFormInputValidation extends BaseValidation {
|
95 | 95 |
|
96 | 96 | public handleSubmit(event: React.FormEvent) {
|
97 | 97 | event.preventDefault();
|
98 |
| - if (this.validateForm(event.target)) { |
99 |
| - super.emit(this.getEvent(this.component.state.fields)); |
100 |
| - } |
| 98 | + this.validateForm(event.target).then(hasError => { |
| 99 | + if (!hasError) { |
| 100 | + super.emit(this.getEvent(this.component.state.fields)); |
| 101 | + } |
| 102 | + }); |
101 | 103 | }
|
102 | 104 |
|
103 | 105 | /**
|
104 | 106 | * Validate the entire html form on submit.
|
105 | 107 | *
|
106 | 108 | * @param form Html form
|
107 | 109 | */
|
108 |
| - private validateForm(form): boolean { |
| 110 | + private validateForm(form): Promise<boolean> { |
109 | 111 | if (!this.component || !this.component.state) {
|
110 | 112 | this.component.state = {
|
111 | 113 | errors: {}
|
112 | 114 | };
|
113 | 115 | }
|
114 | 116 |
|
| 117 | + const validatePromises: Array<Promise<any>> = []; |
| 118 | + |
115 | 119 | form.querySelectorAll("textarea,select,input:not([type='submit']):not([type='file']):not([data-ignore-validation])")
|
116 | 120 | .forEach((element) => {
|
117 |
| - this.validate(element).then((inputErrors) => { |
118 |
| - this.errors = Object.assign(this.errors, inputErrors); |
119 |
| - this.component.setState({ |
120 |
| - errors: this.errors, |
121 |
| - isValidatorUpdate: true |
122 |
| - }); |
123 |
| - }).catch(error => console.error(error)); |
| 121 | + validatePromises.push(this.validate(element)); |
124 | 122 | });
|
125 | 123 |
|
126 |
| - if (Object.keys(this.component.state.errors)[0] && |
127 |
| - form.querySelector(`[name="${Object.keys(this.component.state.errors)[0]}"]`)) { |
128 |
| - form.querySelector(`[name="${Object.keys(this.component.state.errors)[0]}"]`).focus(); |
129 |
| - } |
130 |
| - |
131 |
| - return Object.keys(this.component.state.errors).length === 0; |
| 124 | + return new Promise((resolve) => { |
| 125 | + Promise.all(validatePromises) |
| 126 | + .then((results) => { |
| 127 | + results.forEach((eachResult) => { |
| 128 | + this.errors = Object.assign(this.errors, eachResult); |
| 129 | + this.component.setState({ |
| 130 | + errors: this.errors, |
| 131 | + isValidatorUpdate: true |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + if (Object.keys(this.component.state.errors)[0] && |
| 136 | + form.querySelector(`[name="${Object.keys(this.component.state.errors)[0]}"]`)) { |
| 137 | + form.querySelector(`[name="${Object.keys(this.component.state.errors)[0]}"]`).focus(); |
| 138 | + } |
| 139 | + |
| 140 | + resolve(Object.keys(this.component.state.errors).length === 0); |
| 141 | + }) |
| 142 | + .catch(errors => console.log(errors)); |
| 143 | + }); |
132 | 144 | }
|
133 | 145 |
|
134 | 146 | /**
|
@@ -246,4 +258,7 @@ class ReactFormInputValidation extends BaseValidation {
|
246 | 258 | }
|
247 | 259 | }
|
248 | 260 |
|
| 261 | +export { |
| 262 | + Lang |
| 263 | +}; |
249 | 264 | export default ReactFormInputValidation;
|
0 commit comments