1
1
import React from 'react' ;
2
- import useForm from '../../ lib' ;
2
+ import useForm from 'lib' ;
3
3
import './index.css' ;
4
4
5
5
function Form ( ) {
@@ -12,50 +12,6 @@ function Form() {
12
12
confirm_password : { value : '' , error : '' } ,
13
13
} ;
14
14
15
- // Create your own validationStateSchema
16
- // stateSchema property should be the same in validationStateSchema
17
- // in-order a validation to works in your input.
18
- const stateValidatorSchema = {
19
- first_name : {
20
- required : true ,
21
- validator : {
22
- func : ( value ) => / ^ [ a - z A - Z ] + $ / . test ( value ) ,
23
- error : 'Invalid first name format.' ,
24
- } ,
25
- } ,
26
- last_name : {
27
- required : true ,
28
- validator : {
29
- func : ( value ) => / ^ [ a - z A - Z ] + $ / . test ( value ) ,
30
- error : 'Invalid last name format.' ,
31
- } ,
32
- } ,
33
- tags : {
34
- validator : {
35
- func : ( value ) => / ^ ( , ? \w { 3 , } ) + $ / . test ( value ) ,
36
- error : 'Invalid tag format.' ,
37
- } ,
38
- } ,
39
- password : {
40
- required : true ,
41
- compare : {
42
- to : 'confirm_password' ,
43
- error : 'Password does not match to confirm password' ,
44
- } ,
45
- validator : {
46
- func : ( value ) => / ^ [ a - z A - Z ] + $ / . test ( value ) ,
47
- error : 'Password does not meet the requirement' ,
48
- } ,
49
- } ,
50
- confirm_password : {
51
- required : true ,
52
- validator : {
53
- func : ( value , values ) => value === values . password ,
54
- error : 'Confirm Password does not match to Password' ,
55
- } ,
56
- } ,
57
- } ;
58
-
59
15
const onSubmitForm = ( state ) => {
60
16
alert ( JSON . stringify ( state , null , 2 ) ) ;
61
17
} ;
@@ -67,7 +23,50 @@ function Form() {
67
23
handleOnChange,
68
24
handleOnSubmit,
69
25
disable,
70
- } = useForm ( stateSchema , stateValidatorSchema , onSubmitForm ) ;
26
+ } = useForm (
27
+ stateSchema ,
28
+ {
29
+ first_name : {
30
+ required : true ,
31
+ validator : {
32
+ func : ( value ) => / ^ [ a - z A - Z ] + $ / . test ( value ) ,
33
+ error : 'Invalid first name format.' ,
34
+ } ,
35
+ } ,
36
+ last_name : {
37
+ required : true ,
38
+ validator : {
39
+ func : ( value ) => / ^ [ a - z A - Z ] + $ / . test ( value ) ,
40
+ error : 'Invalid last name format.' ,
41
+ } ,
42
+ } ,
43
+ tags : {
44
+ validator : {
45
+ func : ( value ) => / ^ ( , ? \w { 3 , } ) + $ / . test ( value ) ,
46
+ error : 'Invalid tag format.' ,
47
+ } ,
48
+ } ,
49
+ confirm_password : {
50
+ required : true ,
51
+ validator : {
52
+ func : ( value , values ) => value === values . password ,
53
+ error : 'Confirm Password does not match to Password' ,
54
+ } ,
55
+ } ,
56
+ password : {
57
+ required : true ,
58
+ compare : {
59
+ to : 'confirm_password' ,
60
+ error : 'Password does not match to confirm password' ,
61
+ } ,
62
+ validator : {
63
+ func : ( value ) => / ^ [ a - z A - Z ] + $ / . test ( value ) ,
64
+ error : 'Password does not meet the requirement' ,
65
+ } ,
66
+ } ,
67
+ } ,
68
+ onSubmitForm
69
+ ) ;
71
70
72
71
const { first_name, last_name, tags, password, confirm_password } = values ;
73
72
0 commit comments