Skip to content

Commit 809e7da

Browse files
committed
v1.5.0
1 parent 602dc7b commit 809e7da

File tree

6 files changed

+49
-17
lines changed

6 files changed

+49
-17
lines changed

HOW_TO_USE.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,32 @@
44

55
Component Base to create a form field.
66

7-
| Props | Required | Type | Description |
8-
|-------------------|----------|------------------------|------------------------------------------------------------------------|
9-
| name | false | string | |
10-
| value | false | any | |
11-
| submitted | false | boolean | flag to set if form was submited (also can be set by setFormSubmitted) |
12-
| validation | false | string | rules of validation |
13-
| validationContext | false | object { prop: value } | extra fields for validation bind (ex. required_if) |
14-
| errorMessage | false | string | custom error message from external validation |
7+
| Props | Required | Type | Description |
8+
|--------------------------|----------|------------------------|------------------------------------------------------------------------|
9+
| name | false | string | |
10+
| value | false | any | |
11+
| submitted | false | boolean | flag to set if form was submited (also can be set by setFormSubmitted) |
12+
| validation | false | string | rules of validation |
13+
| validationContext | false | object { prop: value } | extra fields for validation bind (ex. required_if) |
14+
| validationAttributeNames | false | object { prop: value } | see: https://github.com/skaterdav85/validatorjs#custom-attribute-names |
15+
| errorMessage | false | string | custom error message from external validation |
1516

1617
### How to Create a Form Field
1718

1819
```tsx
1920
import FieldCoreBase, { IPropsFieldBase, IStateFieldBase } from '@react-form-fields/core/components/FieldCoreBase';
2021

21-
interface IState extends IStateFieldBase {
22+
interface IState extends IStateFieldBase { //<-- extends
2223
//your state props
2324
}
2425

25-
interface IProps extends IPropsFieldBase {
26+
interface IProps extends IPropsFieldBase { //<-- extends
2627
// your props
2728
onChange: (value: string) => void;
2829
}
2930

3031
class MyComponentField extends FieldCoreBase<IProps, IState> {
31-
//If you need getDerivedStateFromProps dont forget to call super
32+
//[optional] If you need getDerivedStateFromProps dont forget to call super
3233
static getDerivedStateFromProps(props: IProps, currentState: IState): IState {
3334
const state = super.getDerivedStateFromProps(props, currentState);
3435
// your logic....

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@ See [HOW_TO_USE.md](https://github.com/react-form-fields/core/blob/master/HOW_TO
2929
* zipcode
3030
* phone
3131
* document (CNPJ/CPF)
32+
* cnpj
3233
* cpf
33-
* cnpj
34+
* cpf
35+
* cnpj
36+
* money

components/FieldCoreBase.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface IPropsFieldBase {
1616
value?: any;
1717
validation?: string;
1818
validationContext?: Object;
19+
validationAttributeNames?: Object;
1920
errorMessage?: string;
2021
submitted?: boolean;
2122
mask?: string;
@@ -58,7 +59,7 @@ export default abstract class FieldCoreBase<
5859
return this.mask.apply(this.props.value);
5960
}
6061

61-
static getDerivedStateFromProps({ name, value, validation, validationContext, children, submitted }: IPropsFieldBase, currentState: IStateFieldBase): IStateFieldBase {
62+
static getDerivedStateFromProps({ name, value, validation, validationContext, validationAttributeNames, children, submitted }: IPropsFieldBase, currentState: IStateFieldBase): IStateFieldBase {
6263
const customMessages = React.Children
6364
.toArray(children)
6465
.reduce<ErrorMessages>((acc, child: any) => {
@@ -71,7 +72,7 @@ export default abstract class FieldCoreBase<
7172
return acc;
7273
}, {});
7374

74-
const error = validate(name, value, validation, validationContext, customMessages);
75+
const error = validate(name, value, validation, validationContext, validationAttributeNames, customMessages);
7576

7677
return {
7778
...currentState,

config/builder.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { ErrorMessages } from 'validatorjs';
1+
import { AttributeFormatter, ErrorMessages, RegisterAsyncCallback, RegisterCallback } from 'validatorjs';
2+
import Validator = require('validatorjs');
23

34
import { IConfig } from '.';
45
import { IMask } from '../mask';
@@ -20,6 +21,21 @@ export default class ConfigBuilder {
2021
return this;
2122
}
2223

24+
public addValidator(name: string, callback: RegisterCallback, errorMessage: string) {
25+
Validator.register(name, callback, errorMessage);
26+
return this;
27+
}
28+
29+
public addValidatorAsync(name: string, callback: RegisterAsyncCallback, errorMessage: string) {
30+
Validator.registerAsync(name, callback, errorMessage);
31+
return this;
32+
}
33+
34+
public setValidatorAttributeFormatter(func: AttributeFormatter) {
35+
Validator.setAttributeFormatter(func);
36+
return this;
37+
}
38+
2339
public addCustomMessages(lang: string, customMessages: ErrorMessages) {
2440
this.config.validation = { lang, customMessages };
2541
return this;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"validation",
1010
"material"
1111
],
12-
"version": "1.4.9",
12+
"version": "1.5.0",
1313
"main": "./index.js",
1414
"types": "./index.d.ts",
1515
"license": "MIT",

validator/index.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import * as validator from 'validatorjs';
22

3-
export function validate(fieldName: string, value: any, rules: string, context: any = {}, customMessages: validator.ErrorMessages = null): { valid: boolean, message?: string } {
3+
export function validate(
4+
fieldName: string,
5+
value: any,
6+
rules: string,
7+
context: any = {},
8+
attributeNames: any = {},
9+
customMessages: validator.ErrorMessages = null)
10+
: { valid: boolean, message?: string } {
411
if (!rules) return { valid: true };
512

613
fieldName = fieldName || 'value';
714
const result = new validator({ [fieldName]: value, ...context }, { [fieldName]: rules }, customMessages);
815

16+
if (attributeNames) {
17+
result.setAttributeNames(attributeNames);
18+
}
19+
920
if (result.passes()) {
1021
return { valid: true };
1122
}

0 commit comments

Comments
 (0)