1- import { Config , RequestMethod , SimpleValidationErrors , toSimpleValidationErrors , ValidationConfig } from 'laravel-precognition'
1+ import { Config , NamedInputEvent , RequestMethod , SimpleValidationErrors , toSimpleValidationErrors , ValidationConfig , ValidationErrors } from 'laravel-precognition'
22import { useForm as useVueForm } from 'laravel-precognition-vue'
33import { useForm as useInertiaForm } from '@inertiajs/vue3'
44
@@ -16,16 +16,35 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
1616 /**
1717 * Setup event listeners.
1818 */
19- vueForm . validator ( ) . on ( 'errorsChanged' , ( ) => inertiaForm . clearErrors ( ) . setError (
20- // @ts -expect-error
21- toSimpleValidationErrors ( vueForm . validator ( ) . errors ( ) )
22- ) )
19+ vueForm . validator ( ) . on ( 'errorsChanged' , ( ) => {
20+ inertiaClearErrors ( )
21+
22+ inertiaSetError (
23+ // @ts -expect-error
24+ toSimpleValidationErrors ( vueForm . validator ( ) . errors ( ) )
25+ )
26+ } )
2327
2428 /**
2529 * The Inertia submit function.
2630 */
2731 const inertiaSubmit = inertiaForm . submit . bind ( inertiaForm )
2832
33+ /**
34+ * The Inertia reset function.
35+ */
36+ const inertiaReset = inertiaForm . reset . bind ( inertiaForm )
37+
38+ /**
39+ * The Inertia clear errors function.
40+ */
41+ const inertiaClearErrors = inertiaForm . clearErrors . bind ( inertiaForm )
42+
43+ /**
44+ * The Inertia set error function.
45+ */
46+ const inertiaSetError = inertiaForm . setError . bind ( inertiaForm )
47+
2948 /**
3049 * Patch the form.
3150 */
@@ -35,20 +54,60 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
3554 touched : vueForm . touched ,
3655 valid : vueForm . valid ,
3756 invalid : vueForm . invalid ,
38- validate : vueForm . validate ,
39- setValidationTimeout : vueForm . setValidationTimeout ,
40- submit : ( submitMethod : RequestMethod | Config = { } , submitUrl ?: string , submitOptions ?: any ) : void => {
57+ clearErrors ( ) {
58+ inertiaClearErrors ( )
59+
60+ vueForm . setErrors ( { } )
61+
62+ return this
63+ } ,
64+ reset ( ...names : string [ ] ) {
65+ inertiaReset ( ...names )
66+
67+ vueForm . reset ( ...names )
68+ } ,
69+ setErrors ( errors : SimpleValidationErrors | ValidationErrors ) {
70+ // @ts -expect-error
71+ vueForm . setErrors ( errors )
72+
73+ return this
74+ } ,
75+ setError ( key : any , value ?: any ) {
76+ this . setErrors ( {
77+ ...inertiaForm . errors ,
78+ ...typeof value === 'undefined'
79+ ? key
80+ : { [ key ] : value }
81+ } )
82+
83+ return this
84+ } ,
85+ validate ( name : string | NamedInputEvent ) {
86+ vueForm . setData ( inertiaForm . data ( ) )
87+
88+ vueForm . validate ( name )
89+
90+ return this
91+ } ,
92+ setValidationTimeout ( duration : number ) {
93+ vueForm . setValidationTimeout ( duration )
94+
95+ return this
96+ } ,
97+ submit ( submitMethod : RequestMethod | Config = { } , submitUrl ?: string , submitOptions ?: any ) : void {
4198 const isPatchedCall = typeof submitMethod !== 'string'
4299
100+ const userOptions = isPatchedCall
101+ ? submitMethod
102+ : submitOptions
103+
43104 const options = {
44- ...isPatchedCall
45- ? submitMethod
46- : submitOptions ,
105+ ...userOptions ,
47106 onError : ( errors : SimpleValidationErrors ) : any => {
48107 vueForm . validator ( ) . setErrors ( errors )
49108
50- if ( submitOptions . onError ) {
51- return submitOptions . onError ( errors )
109+ if ( userOptions . onError ) {
110+ return userOptions . onError ( errors )
52111 }
53112 } ,
54113 }
@@ -59,6 +118,6 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
59118 ( isPatchedCall ? url : submitUrl ) ,
60119 options
61120 )
62- }
121+ } ,
63122 } )
64123}
0 commit comments