Skip to content

Commit 8dafbac

Browse files
Merge pull request #10 from gokulakannant/develop
Develop
2 parents 03a19d8 + eb8a335 commit 8dafbac

File tree

11 files changed

+72
-42
lines changed

11 files changed

+72
-42
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

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ dist/typedocconfig.d.ts
1414
Documentation/
1515
node_modules/
1616
.travis.yml
17+
theme/

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ All notable changes to React Form Input Validation APIs will be documented in th
88

99
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1010

11+
## [2.0.1] - 25/11/2019
12+
13+
### Fixed
14+
15+
- Fixed `onformsubmit` event is received with empty values.
16+
1117
## [2.0.0] - 22/11/2019
1218

1319
### Added

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# React Input Form Validation
1+
# React Form Input Validation
22

33
[![npm package](https://img.shields.io/npm/v/react-form-input-validation)](https://www.npmjs.com/package/react-form-input-validation)
44
[![Build Status](https://api.travis-ci.org/gokulakannant/react-form-input-validation.png?branch=master)](https://travis-ci.org/gokulakannant/react-form-input-validation)
@@ -7,7 +7,7 @@
77
A customized [validatorjs](https://www.npmjs.com/package/validatorjs) library to validate the react forms. It uses the [Controlled Components](https://reactjs.org/docs/forms.html#controlled-components) approach for validation.
88

99
* [Supported Rules](https://www.npmjs.com/package/validatorjs#available-rules) (It is supports all validatorjs rules)
10-
* [Documentation](https://gokulakannant.github.io/react-form-input-validation/v2.0.0/index.html)
10+
* [Documentation](https://gokulakannant.github.io/react-form-input-validation/v2.0.1/index.html)
1111
* [Demo](https://codesandbox.io/s/react-form-input-validation-demp-hyuju?fontsize=14&hidenavigation=1&theme=dark) (in CodeSandbox)
1212

1313
## Why use react-form-input-validation?
@@ -36,7 +36,7 @@ Using [yarn](https://yarnpkg.com/en/) as your package manager.
3636

3737
## Usage
3838

39-
A example form has given below. View all available apis in [documentation](https://gokulakannant.github.io/react-form-input-validation/v2.0.0/index.html).
39+
A example form has given below. View all available apis in [documentation](https://gokulakannant.github.io/react-form-input-validation/v2.0.1/index.html).
4040

4141
```js
4242
import React from "react";
@@ -142,7 +142,7 @@ The output will be like, "The Username field is required.".
142142

143143
## Supported form fields
144144

145-
|Form Fields and Attributes|Support By Library|
145+
|Form Fields and Attributes|Supported By Library|
146146
| :-- |:--:|
147147
|text|☑|
148148
|password|☑|

VERSIONS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The most recent version is recommended in production.
88

99
|Versions|Documents|Releases|
1010
|--------|---------|--------|
11+
|v2.0.1|[Documentation](https://gokulakannant.github.io/react-form-input-validation/v2.0.1/index.html)|[Release notes](https://github.com/gokulakannant/react-form-input-validation/releases/tag/v2.0.1)|
1112
|v2.0.0|[Documentation](https://gokulakannant.github.io/react-form-input-validation/v2.0.0/index.html)|[Release notes](https://github.com/gokulakannant/react-form-input-validation/releases/tag/v2.0.0)|
1213
|v1.0.1|[Documentation](https://gokulakannant.github.io/react-form-input-validation/index.html)| |
1314

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"dependencies": {
66
"react": "^16.8.0",
77
"react-dom": "^16.8.0",
8-
"react-form-input-validation": "^1.0.0",
8+
"react-form-input-validation": "^2.0.1",
99
"react-scripts": "3.2.0"
1010
},
1111
"scripts": {

example/src/Form.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import ReactFormValidation from "react-form-input-validation";
2+
import ReactFormValidation, { Lang } from "react-form-input-validation";
33
import "./Form.css";
44

55
class ValidationForm extends React.Component {
@@ -44,10 +44,12 @@ class ValidationForm extends React.Component {
4444
passes();
4545
}, 1000);
4646
});
47-
/* let messages = ReactFormValidation.getMessages('en');
48-
messages.required = 'Whoops, :attribute field is required.';
47+
let messages = ReactFormValidation.getMessages(Lang.en);
48+
console.log(messages);
49+
/* messages.required = 'Whoops, :attribute field is required.';
4950
ReactFormValidation.setMessages('en', messages);
50-
ReactFormValidation.useLang('en') */
51+
console.log(Lang)
52+
ReactFormValidation.useLang(Lang.fr) */
5153
}
5254

5355
render() {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "react-form-input-validation",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "A validator package to validate the react forms",
55
"main": "./dist/bundle.js",
66
"license": "GPLv3",
7-
"author": "gokulakannant <gokulakannanthangaraji@gmail.com>",
7+
"author": "gokulakannant <gokulakannanthangaraji@gmail.com> (https://gokulakannant.github.io)",
8+
"homepage": "https://gokulakannant.github.io/react-form-input-validation/v2.0.1/index.html",
89
"repository": {
910
"type": "git",
1011
"url": "https://github.com/gokulakannant/react-form-input-validation.git"

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)