1
- export const Formatic = ( { validate_url } ) => ( {
2
- error : false ,
3
- errors : [ ] ,
4
- sending : false ,
5
- success : false ,
1
+ export const Formatic = ( ) => ( {
6
2
steps : [ ] ,
7
3
currentStep : 1 ,
8
4
redirect_url : null ,
9
5
submit_url : null ,
6
+ success : false ,
7
+ form : "" ,
8
+
10
9
init ( ) {
11
10
this . $nextTick ( ( ) => {
12
11
if (
@@ -19,74 +18,92 @@ export const Formatic = ({ validate_url }) => ({
19
18
this . steps = JSON . parse ( window . requiredSteps ) ;
20
19
} ) ;
21
20
22
- this . submit_url = validate_url ;
21
+ this . submit_url = window . location . href ;
22
+
23
+ this . form = this . $form (
24
+ "post" ,
25
+ this . $refs . form . getAttribute ( "action" ) ,
26
+ JSON . parse ( this . $refs . form . getAttribute ( "x-data" ) ) . dynamic_form
27
+ ) ;
23
28
} ,
24
- sendForm : async function ( ) {
25
- this . sending = true ;
26
- const formData = new FormData ( this . $refs . form ) ;
27
-
28
- // Post the form.
29
- return fetch ( this . resolveFormUrl ( ) , {
30
- headers : {
31
- "X-Requested-With" : "XMLHttpRequest" ,
32
- } ,
33
- method : "POST" ,
34
- body : new FormData ( this . $refs . form ) ,
35
- } )
36
- . then ( ( res ) => res . json ( ) )
37
- . then ( ( json ) => {
38
- if (
39
- json [ "success" ] &&
40
- this . isMultiStep ( ) &&
41
- ! this . isLastStep ( )
42
- ) {
43
- this . errors = [ ] ;
44
- this . error = false ;
45
- this . sending = false ;
46
-
47
- return ;
48
- }
49
-
50
- if ( json [ "success" ] ) {
51
- this . errors = [ ] ;
52
- this . success = true ;
53
- this . error = false ;
54
- this . sending = false ;
55
- this . $refs . form . reset ( ) ;
56
-
57
- setTimeout ( ( ) => {
58
- this . success = false ;
59
-
60
- if ( this . redirect_url ) {
61
- window . location . href = this . redirect_url ;
62
- }
63
- } , 1000 ) ;
64
- }
65
-
66
- if ( json [ "error" ] ) {
67
- this . sending = false ;
68
- this . error = true ;
69
- this . success = false ;
70
- this . errors = json [ "error" ] ;
71
- }
72
- } )
73
- . catch ( ( err ) => {
74
- console . log ( "err" , err ) ;
75
- this . sending = false ;
29
+
30
+ submit : async function ( ) {
31
+ try {
32
+ const response = await this . form . submit ( {
33
+ headers : { "Content-Type" : "multipart/form-data" } ,
76
34
} ) ;
35
+
36
+ if (
37
+ response . status === 200 &&
38
+ this . isMultiStep ( ) &&
39
+ ! this . isLastStep ( )
40
+ ) {
41
+ this . form . errors = { } ;
42
+
43
+ return ;
44
+ }
45
+
46
+ if ( response . status === 200 ) {
47
+ this . form . reset ( ) ;
48
+ this . form . errors = { } ;
49
+ this . success = true ;
50
+
51
+ setTimeout ( ( ) => {
52
+ this . success = false ;
53
+
54
+ if ( this . redirect_url ) {
55
+ window . location . href = this . redirect_url ;
56
+ }
57
+
58
+ if ( this . isMultiStep ( ) ) {
59
+ location . reload ( ) ;
60
+ }
61
+ } , 1000 ) ;
62
+ }
63
+ } catch ( error ) {
64
+ if ( error . response && error . response . data . errors ) {
65
+ this . form . errors = error . response . data . errors ;
66
+ }
67
+ }
77
68
} ,
69
+
78
70
prev ( ) {
79
71
this . currentStep -- ;
80
72
} ,
81
73
next : async function ( ) {
82
- await this . sendForm ( ) ;
74
+ const isValid = this . validateFields ( ) ;
75
+
76
+ // Move to the next step only if the form is valid
77
+ if ( isValid ) {
78
+ if ( ! ( this . form . errors && this . hasRequiredFields ( ) ) ) {
79
+ this . currentStep ++ ;
80
+ this . form . errors = { } ;
81
+ }
83
82
84
- if ( ! ( this . error && this . hasRequiredFields ( ) ) ) {
85
- this . currentStep ++ ;
86
- this . errors = [ ] ;
83
+ window . scrollTo ( { top : 0 , behavior : "smooth" } ) ;
87
84
}
85
+ } ,
86
+ validateFields ( ) {
87
+ this . form . errors = { } ;
88
+
89
+ const requiredFields = Object . values ( this . steps [ this . currentStep - 1 ] ) ;
90
+
91
+ requiredFields . forEach ( ( field ) => {
92
+ // Grab inputs
93
+ const input = document . querySelector ( `[name="${ field } "]` ) ;
94
+
95
+ // Format field's error message
96
+ let formattedErrorMessage = `The ${ field
97
+ . replace ( / _ / g, " " )
98
+ . replace ( / \b \w / g, ( c ) => c . toUpperCase ( ) ) } field is required.`;
99
+
100
+ // Add error to errors object (if input is empty)
101
+ if ( input && input . offsetParent && ! this . form [ field ] ) {
102
+ this . form . errors [ field ] = formattedErrorMessage ;
103
+ }
104
+ } ) ;
88
105
89
- window . scrollTo ( { top : 0 , behavior : "smooth" } ) ;
106
+ return ! Object . keys ( this . form . errors ) . length ;
90
107
} ,
91
108
isMultiStep ( ) {
92
109
return this . steps . length > 0 ;
@@ -103,7 +120,7 @@ export const Formatic = ({ validate_url }) => ({
103
120
} ,
104
121
hasRequiredFields ( ) {
105
122
const fields = Object . values ( this . steps [ this . currentStep - 1 ] ) . filter (
106
- ( field ) => ( this . errors . hasOwnProperty ( field ) ? true : false )
123
+ ( field ) => ( this . form . errors . hasOwnProperty ( field ) ? true : false )
107
124
) ;
108
125
109
126
return fields . length > 0 ;
0 commit comments