1
1
import React from "react" ;
2
- import ReactFormValidation from "react-form-validation" ;
2
+ import ReactFormValidation from "react-form-input- validation" ;
3
3
import "./Form.css" ;
4
4
5
5
class ValidationForm extends React . Component {
@@ -17,33 +17,53 @@ class ValidationForm extends React.Component {
17
17
dropoff_place : "" ,
18
18
extras : [ ]
19
19
} ,
20
- inputErrors : { }
20
+ errors : { }
21
21
} ;
22
- this . form = new ReactFormValidation (
23
- this ,
24
- {
25
- customer_name : "required" ,
26
- email_address : "required|email" ,
27
- phone_number : "required|numeric|digits_between:10,12" ,
28
- pickup_time : "required|date" ,
29
- taxi : "required" ,
30
- pickup_place : "required" ,
31
- comments : "required|max:20" ,
32
- dropoff_place : "required" ,
33
- extras : "required|array"
34
- } ,
35
- ( fields ) => {
36
- // Place to dispatch the form submit actions
37
- alert ( JSON . stringify ( fields ) ) ;
38
- }
39
- ) ;
22
+ this . form = new ReactFormValidation ( this , { locale : "en" } ) ;
23
+ this . form . useRules ( {
24
+ customer_name : "required|username_available" ,
25
+ email_address : "required|email" ,
26
+ phone_number : "required|numeric|digits_between:10,12" ,
27
+ pickup_time : "required|date" ,
28
+ taxi : "required" ,
29
+ pickup_place : "required" ,
30
+ comments : "required|max:20" ,
31
+ dropoff_place : "required" ,
32
+ extras : "required|array"
33
+ } ) ;
34
+
35
+ this . form . addEventListener ( "reactformsubmit" , ( fields ) => {
36
+ console . log ( "1st reactsubmit" , fields ) ;
37
+ } ) ;
38
+
39
+ this . form . addEventListener ( "reactformsubmit" , ( fields ) => {
40
+ console . log ( "2nd reactsubmit" , fields ) ;
41
+ } ) ;
42
+
43
+ this . form . onreactformsubmit = ( fields ) => {
44
+ console . log ( "1st onreactformsubmit" , fields ) ;
45
+ }
46
+
47
+ this . form . onreactformsubmit = ( fields ) => {
48
+ console . log ( "2nd onreactformsubmit" , fields ) ;
49
+ }
50
+
51
+ ReactFormValidation . registerAsync ( 'username_available' , function ( username , attribute , req , passes ) {
52
+ setTimeout ( ( ) => {
53
+ if ( username === "foo" )
54
+ passes ( false , 'Username has already been taken.' ) ; // if username is not available
55
+ else
56
+ passes ( ) ;
57
+ } , 1000 ) ;
58
+ } ) ;
40
59
/* let messages = ReactFormValidation.getMessages('en');
41
60
messages.required = 'Whoops, :attribute field is required.';
42
61
ReactFormValidation.setMessages('en', messages);
43
62
ReactFormValidation.useLang('en') */
44
63
}
45
64
46
65
render ( ) {
66
+ console . log ( this . state )
47
67
return (
48
68
< div style = { { maxWidth : "600px" , margin : "0 auto" } } >
49
69
< h3 > React Input Form Validation</ h3 >
@@ -60,15 +80,16 @@ class ValidationForm extends React.Component {
60
80
type = "text"
61
81
name = "customer_name"
62
82
onBlur = { this . form . handleBlurEvent }
63
- onChange = { this . form . handleFieldsChange }
83
+ onChange = { this . form . handleChangeEvent }
64
84
value = { this . state . fields . customer_name }
65
85
// To override the attribute name
66
86
data-attribute-name = "CUSTOMER NAME"
87
+ data-async
67
88
/>
68
89
</ label >
69
90
< label className = "error" >
70
- { this . state . inputErrors . customer_name
71
- ? this . state . inputErrors . customer_name . message
91
+ { this . state . errors . customer_name
92
+ ? this . state . errors . customer_name
72
93
: "" }
73
94
</ label >
74
95
</ p >
@@ -80,13 +101,13 @@ class ValidationForm extends React.Component {
80
101
type = "tel"
81
102
name = "phone_number"
82
103
onBlur = { this . form . handleBlurEvent }
83
- onChange = { this . form . handleFieldsChange }
104
+ onChange = { this . form . handleChangeEvent }
84
105
value = { this . state . fields . phone_number }
85
106
/>
86
107
</ label >
87
108
< label className = "error" >
88
- { this . state . inputErrors . phone_number
89
- ? this . state . inputErrors . phone_number . message
109
+ { this . state . errors . phone_number
110
+ ? this . state . errors . phone_number
90
111
: "" }
91
112
</ label >
92
113
</ p >
@@ -98,13 +119,13 @@ class ValidationForm extends React.Component {
98
119
type = "email"
99
120
name = "email_address"
100
121
onBlur = { this . form . handleBlurEvent }
101
- onChange = { this . form . handleFieldsChange }
122
+ onChange = { this . form . handleChangeEvent }
102
123
value = { this . state . fields . email_address }
103
124
/>
104
125
</ label >
105
126
< label className = "error" >
106
- { this . state . inputErrors . email_address
107
- ? this . state . inputErrors . email_address . message
127
+ { this . state . errors . email_address
128
+ ? this . state . errors . email_address
108
129
: "" }
109
130
</ label >
110
131
</ p >
@@ -117,7 +138,7 @@ class ValidationForm extends React.Component {
117
138
< input
118
139
type = "radio"
119
140
name = "taxi"
120
- onChange = { this . form . handleFieldsChange }
141
+ onChange = { this . form . handleChangeEvent }
121
142
value = "car"
122
143
/> { " " }
123
144
Car{ " " }
@@ -129,7 +150,7 @@ class ValidationForm extends React.Component {
129
150
< input
130
151
type = "radio"
131
152
name = "taxi"
132
- onChange = { this . form . handleFieldsChange }
153
+ onChange = { this . form . handleChangeEvent }
133
154
value = "van"
134
155
/> { " " }
135
156
Van{ " " }
@@ -141,15 +162,15 @@ class ValidationForm extends React.Component {
141
162
< input
142
163
type = "radio"
143
164
name = "taxi"
144
- onChange = { this . form . handleFieldsChange }
165
+ onChange = { this . form . handleChangeEvent }
145
166
value = "tuk tuk"
146
167
/> { " " }
147
168
Tuk Tuk{ " " }
148
169
</ label >
149
170
</ p >
150
171
< label className = "error" >
151
- { this . state . inputErrors . taxi
152
- ? this . state . inputErrors . taxi . message
172
+ { this . state . errors . taxi
173
+ ? this . state . errors . taxi
153
174
: "" }
154
175
</ label >
155
176
</ fieldset >
@@ -162,7 +183,7 @@ class ValidationForm extends React.Component {
162
183
< input
163
184
type = "checkbox"
164
185
name = "extras"
165
- onChange = { this . form . handleFieldsChange }
186
+ onChange = { this . form . handleChangeEvent }
166
187
value = "baby"
167
188
/> { " " }
168
189
Baby Seat{ " " }
@@ -174,7 +195,7 @@ class ValidationForm extends React.Component {
174
195
< input
175
196
type = "checkbox"
176
197
name = "extras"
177
- onChange = { this . form . handleFieldsChange }
198
+ onChange = { this . form . handleChangeEvent }
178
199
value = "wheelchair"
179
200
/> { " " }
180
201
Wheelchair Access{ " " }
@@ -186,15 +207,15 @@ class ValidationForm extends React.Component {
186
207
< input
187
208
type = "checkbox"
188
209
name = "extras"
189
- onChange = { this . form . handleFieldsChange }
210
+ onChange = { this . form . handleChangeEvent }
190
211
value = "tip"
191
212
/> { " " }
192
213
Stock Tip{ " " }
193
214
</ label >
194
215
</ p >
195
216
< label className = "error" >
196
- { this . state . inputErrors . extras
197
- ? this . state . inputErrors . extras . message
217
+ { this . state . errors . extras
218
+ ? this . state . errors . extras
198
219
: "" }
199
220
</ label >
200
221
</ fieldset >
@@ -205,13 +226,13 @@ class ValidationForm extends React.Component {
205
226
< input
206
227
type = "date"
207
228
name = "pickup_time"
208
- onChange = { this . form . handleFieldsChange }
229
+ onChange = { this . form . handleChangeEvent }
209
230
value = { this . state . fields . pickup_time }
210
231
/>
211
232
</ label >
212
233
< label className = "error" >
213
- { this . state . inputErrors . pickup_time
214
- ? this . state . inputErrors . pickup_time . message
234
+ { this . state . errors . pickup_time
235
+ ? this . state . errors . pickup_time
215
236
: "" }
216
237
</ label >
217
238
</ p >
@@ -223,7 +244,7 @@ class ValidationForm extends React.Component {
223
244
id = "pickup_place"
224
245
name = "pickup_place"
225
246
value = { this . state . fields . pickup_place }
226
- onChange = { this . form . handleFieldsChange }
247
+ onChange = { this . form . handleChangeEvent }
227
248
>
228
249
< option value = "" > Select One</ option >
229
250
< option value = "office" > Taxi Office</ option >
@@ -232,8 +253,8 @@ class ValidationForm extends React.Component {
232
253
</ select >
233
254
</ label >
234
255
< label className = "error" >
235
- { this . state . inputErrors . pickup_place
236
- ? this . state . inputErrors . pickup_place . message
256
+ { this . state . errors . pickup_place
257
+ ? this . state . errors . pickup_place
237
258
: "" }
238
259
</ label >
239
260
</ p >
@@ -245,7 +266,7 @@ class ValidationForm extends React.Component {
245
266
type = "text"
246
267
name = "dropoff_place"
247
268
value = { this . state . fields . dropoff_place }
248
- onChange = { this . form . handleFieldsChange }
269
+ onChange = { this . form . handleChangeEvent }
249
270
list = "destinations"
250
271
/>
251
272
</ label >
@@ -256,8 +277,8 @@ class ValidationForm extends React.Component {
256
277
< option value = "Fred Flinstone's House" />
257
278
</ datalist >
258
279
< label className = "error" >
259
- { this . state . inputErrors . dropoff_place
260
- ? this . state . inputErrors . dropoff_place . message
280
+ { this . state . errors . dropoff_place
281
+ ? this . state . errors . dropoff_place
261
282
: "" }
262
283
</ label >
263
284
</ p >
@@ -269,13 +290,13 @@ class ValidationForm extends React.Component {
269
290
name = "comments"
270
291
maxLength = "20"
271
292
value = { this . state . fields . comments }
272
- onChange = { this . form . handleFieldsChange }
293
+ onChange = { this . form . handleChangeEvent }
273
294
onBlur = { this . form . handleBlurEvent }
274
295
> </ textarea >
275
296
</ label >
276
297
< label className = "error" >
277
- { this . state . inputErrors . comments
278
- ? this . state . inputErrors . comments . message
298
+ { this . state . errors . comments
299
+ ? this . state . errors . comments
279
300
: "" }
280
301
</ label >
281
302
</ p >
0 commit comments