@@ -25,21 +25,24 @@ function useForm(
25
25
// Get a local copy of stateSchema
26
26
useEffect ( ( ) => {
27
27
setStateSchema ( stateSchema ) ;
28
- setDisable ( true ) ; // Disable button in initial render.
28
+
29
29
setInitialErrorState ( ) ;
30
30
} , [ ] ) ; // eslint-disable-line
31
31
32
- // Set a brand new field values and errors
32
+ // Set a brand new field values and errors
33
33
// If stateSchema changes
34
34
useEffect ( ( ) => {
35
35
const values = get_prop_values ( state , VALUE ) ;
36
+ const errors = Object . keys ( values ) . reduce ( ( accu , curr ) => {
37
+ accu [ curr ] = validateField ( curr , values [ curr ] ) ;
38
+ return accu ;
39
+ } , { } ) ;
40
+
41
+ // Marked form as dirty if state was changed.
42
+ setIsDirty ( true ) ;
43
+
36
44
setValues ( values ) ;
37
- setErrors (
38
- Object . keys ( values ) . reduce ( ( accu , curr ) => {
39
- accu [ curr ] = validateField ( curr , values [ curr ] ) ;
40
- return accu ;
41
- } , { } )
42
- ) ;
45
+ setErrors ( errors ) ;
43
46
} , [ state ] ) ; // eslint-disable-line
44
47
45
48
// For every changed in our state this will be fired
@@ -50,35 +53,41 @@ function useForm(
50
53
}
51
54
} , [ errors , isDirty ] ) ; // eslint-disable-line
52
55
53
- // Set a value of a specific field
56
+ // Set field value to specific field.
54
57
const setFieldValue = ( { name, value } ) => {
55
- setValues ( prevState => ( { ...prevState , [ name ] : value } ) ) ;
56
- setDirty ( prevState => ( { ...prevState , [ name ] : true } ) ) ;
58
+ setValues ( ( prevState ) => ( { ...prevState , [ name ] : value } ) ) ;
59
+ setDirty ( ( prevState ) => ( { ...prevState , [ name ] : true } ) ) ;
57
60
} ;
58
61
59
- // Set an error of a specific field
60
- const setFieldError = ( { name, error } ) =>
61
- setErrors ( prevState => ( { ...prevState , [ name ] : error } ) ) ;
62
+ // Set to specific field.
63
+ const setFieldError = ( { name, error } ) => {
64
+ setErrors ( ( prevState ) => ( { ...prevState , [ name ] : error } ) ) ;
65
+ } ;
62
66
63
- // Validate fields in forms
67
+ // Function used to validate form fields
64
68
const validateField = useCallback (
65
69
( name , value ) => {
66
- const validator = stateValidatorSchema ;
70
+ const fieldValidator = stateValidatorSchema [ name ] ;
67
71
// Making sure that stateValidatorSchema name is same in
68
72
// stateSchema
69
- if ( ! validator [ name ] ) return ;
70
-
71
- const field = validator [ name ] ;
73
+ if ( ! fieldValidator ) {
74
+ return ;
75
+ }
72
76
73
77
let error = '' ;
74
- error = is_required ( value , field . required ) ;
78
+ error = is_required ( value , fieldValidator [ ' required' ] ) ;
75
79
76
- if ( is_object ( field [ 'validator' ] ) && error === '' ) {
77
- const validateFieldByCallback = field [ 'validator' ] ;
80
+ // Bail out if field is not required and no value set.
81
+ // To prevent proceeding to validator function
82
+ if ( ! fieldValidator [ 'required' ] && ! value ) {
83
+ return error ;
84
+ }
78
85
86
+ // Run custom validator function
87
+ if ( error === '' && is_object ( fieldValidator [ 'validator' ] ) ) {
79
88
// Test the function callback if the value is meet the criteria
80
- if ( ! validateFieldByCallback [ 'func' ] ( value , values ) ) {
81
- error = validateFieldByCallback [ 'error' ] ;
89
+ if ( ! fieldValidator [ 'validator' ] [ 'func' ] ( value , values ) ) {
90
+ error = fieldValidator [ 'validator' ] [ 'error' ] ;
82
91
}
83
92
}
84
93
@@ -90,7 +99,7 @@ function useForm(
90
99
// Set Initial Error State
91
100
// When hooks was first rendered...
92
101
const setInitialErrorState = useCallback ( ( ) => {
93
- Object . keys ( errors ) . map ( name =>
102
+ Object . keys ( errors ) . map ( ( name ) =>
94
103
setFieldError ( { name, error : validateField ( name , values [ name ] ) } )
95
104
) ;
96
105
} , [ errors , values , validateField ] ) ;
@@ -100,14 +109,14 @@ function useForm(
100
109
// Wrapped in useCallback to cached the function to avoid intensive memory leaked
101
110
// in every re-render in component
102
111
const validateErrorState = useCallback (
103
- ( ) => Object . values ( errors ) . some ( error => error ) ,
112
+ ( ) => Object . values ( errors ) . some ( ( error ) => error ) ,
104
113
[ errors ]
105
114
) ;
106
115
107
116
// Use this callback function to safely submit the form
108
117
// without any errors in state...
109
118
const handleOnSubmit = useCallback (
110
- event => {
119
+ ( event ) => {
111
120
event . preventDefault ( ) ;
112
121
113
122
// Making sure that there's no error in the state
@@ -121,9 +130,7 @@ function useForm(
121
130
122
131
// Event handler for handling changes in input.
123
132
const handleOnChange = useCallback (
124
- event => {
125
- setIsDirty ( true ) ;
126
-
133
+ ( event ) => {
127
134
const name = event . target . name ;
128
135
const value = event . target . value ;
129
136
0 commit comments