Skip to content

Commit 14f3b58

Browse files
committed
Fixup! onformsubmit empty value returning issue has fixed
1) onformsubmit issue fixed 2) Lang enum has exported for public use 3) Updated the corresponding non breaking changes in the documents Signed-off-by: gokulakannant <[email protected]>
1 parent 9f24e74 commit 14f3b58

File tree

3 files changed

+49
-30
lines changed

3 files changed

+49
-30
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
dist
33
Documentation
4+
example/package-lock.json

src/index.ts

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Validator = require("validatorjs");
22
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";
44

55
class ReactFormInputValidation extends BaseValidation {
66
private component: IReactComponent;
@@ -29,15 +29,15 @@ class ReactFormInputValidation extends BaseValidation {
2929
Validator.registerAsync(name, callbackFn);
3030
}
3131

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);
3434
}
3535

36-
static getMessages(name: string): object {
37-
return Validator.getMessages(name);
36+
static getMessages(langCode: Lang): object {
37+
return Validator.getMessages(langCode);
3838
}
3939

40-
static getDefaultLang(): string {
40+
static getDefaultLang(): Lang {
4141
return Validator.getDefaultLang();
4242
}
4343

@@ -95,40 +95,52 @@ class ReactFormInputValidation extends BaseValidation {
9595

9696
public handleSubmit(event: React.FormEvent) {
9797
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+
});
101103
}
102104

103105
/**
104106
* Validate the entire html form on submit.
105107
*
106108
* @param form Html form
107109
*/
108-
private validateForm(form): boolean {
110+
private validateForm(form): Promise<boolean> {
109111
if (!this.component || !this.component.state) {
110112
this.component.state = {
111113
errors: {}
112114
};
113115
}
114116

117+
const validatePromises: Array<Promise<any>> = [];
118+
115119
form.querySelectorAll("textarea,select,input:not([type='submit']):not([type='file']):not([data-ignore-validation])")
116120
.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));
124122
});
125123

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+
});
132144
}
133145

134146
/**
@@ -246,4 +258,7 @@ class ReactFormInputValidation extends BaseValidation {
246258
}
247259
}
248260

261+
export {
262+
Lang
263+
};
249264
export default ReactFormInputValidation;

src/specs/react-form-input-validator.spec.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -403,31 +403,34 @@ export abstract class ReactFormInputValidation extends EventTarget {
403403
/**
404404
* You can also add your own custom language by calling setMessages.
405405
*
406-
* @param name The name of the rule.
406+
* @param langCode Override {@link Lang} code validation error messages.
407407
* @param values A error messages object.
408408
* @example
409409
* ```js
410410
*
411-
* ReactFormInputValidation.setMessages('lang_code', {
411+
* import ReactFormInputValidation, { Lang } from "react-form-input-validation";
412+
*
413+
* ReactFormInputValidation.setMessages(Lang.en, {
412414
* required: 'The :attribute field is required.'
413415
* });
414416
* ```
415417
* @see Example in [Code Sandbox](https://codesandbox.io/s/react-form-input-validationattribute-formattors-bfomi).
416418
*/
417-
static setMessages(name: string, values: object): void { }
419+
static setMessages(langCode: Lang, values: object): void { }
418420

419421
/**
420422
* Get the raw object of messages for the given language.
421423
*
422-
* @param name The name of the rule.
424+
* @param langCode Retrive {@link Lang} code validation error messages.
423425
* @example
424426
* ```js
425427
*
426-
* ReactFormInputValidation.getMessages('lang_code');
428+
* import ReactFormInputValidation, { Lang } from "react-form-input-validation";
429+
* ReactFormInputValidation.getMessages(Lang.en); (i.e) 'en'
427430
* ```
428431
* @see Example in [Code Sandbox](https://codesandbox.io/s/react-form-input-validationattribute-formattors-bfomi).
429432
*/
430-
static getMessages(name: string): object { return _; }
433+
static getMessages(langCode: Lang): object { return _; }
431434

432435
/***
433436
* Get the default language being used.
@@ -438,7 +441,7 @@ export abstract class ReactFormInputValidation extends EventTarget {
438441
* ```
439442
* @see Example in [Code Sandbox](https://codesandbox.io/s/react-form-input-validationlocale-rhz6w).
440443
*/
441-
static getDefaultLang(): string { return _; }
444+
static getDefaultLang(): Lang { return _; }
442445

443446
/**
444447
* You can supply global custom attribute names in your app with the attributes property.

0 commit comments

Comments
 (0)