From a0adb503de53dd9749b1d82813ff71629a182789 Mon Sep 17 00:00:00 2001 From: Verox001 Date: Sat, 1 Feb 2025 02:23:41 +0100 Subject: [PATCH 1/2] Fixed TypeError on providing only a partial subset of form fields --- packages/react/src/useForm.ts | 6 +++--- playgrounds/react/resources/js/Pages/Form.tsx | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/react/src/useForm.ts b/packages/react/src/useForm.ts index 0e0188e60..000350973 100644 --- a/packages/react/src/useForm.ts +++ b/packages/react/src/useForm.ts @@ -26,7 +26,7 @@ export interface InertiaFormProps { reset: (...fields: (keyof TForm)[]) => void clearErrors: (...fields: (keyof TForm)[]) => void setError(field: keyof TForm, value: string): void - setError(errors: Record): void + setError(errors: Partial>): void submit: (method: Method, url: string, options?: FormOptions) => void get: (url: string, options?: FormOptions) => void patch: (url: string, options?: FormOptions) => void @@ -216,13 +216,13 @@ export default function useForm( ) const setError = useCallback( - (fieldOrFields: keyof TForm | Record, maybeValue?: string) => { + (fieldOrFields: keyof TForm | Partial>, maybeValue?: string) => { setErrors((errors) => { const newErrors = { ...errors, ...(typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } - : (fieldOrFields as Record)), + : (fieldOrFields as Partial>)), } setHasErrors(Object.keys(newErrors).length > 0) return newErrors diff --git a/playgrounds/react/resources/js/Pages/Form.tsx b/playgrounds/react/resources/js/Pages/Form.tsx index 50cc078b5..a6f8b09c3 100644 --- a/playgrounds/react/resources/js/Pages/Form.tsx +++ b/playgrounds/react/resources/js/Pages/Form.tsx @@ -11,6 +11,11 @@ const Form = () => { function submit(e) { e.preventDefault() form.post('/user') + + form.setError({ + 'name': 'Name is required', + 'company': 'Company is required', + }) } return ( From ac520829e43a9747eb61de536bde77b4d973d652 Mon Sep 17 00:00:00 2001 From: Verox001 Date: Sat, 1 Feb 2025 02:27:55 +0100 Subject: [PATCH 2/2] Fixed types for vue and svelte as well --- packages/svelte/src/useForm.ts | 6 +++--- packages/vue3/src/useForm.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/svelte/src/useForm.ts b/packages/svelte/src/useForm.ts index 8cbd4c715..d484bc8e3 100644 --- a/packages/svelte/src/useForm.ts +++ b/packages/svelte/src/useForm.ts @@ -36,7 +36,7 @@ export interface InertiaFormProps { reset(...fields: (keyof TForm)[]): this clearErrors(...fields: (keyof TForm)[]): this setError(field: keyof TForm, value: string): this - setError(errors: Errors): this + setError(errors: Partial): this submit(method: Method, url: string, options?: FormOptions): void get(url: string, options?: FormOptions): void post(url: string, options?: FormOptions): void @@ -120,10 +120,10 @@ export default function useForm( return this }, - setError(fieldOrFields: keyof TForm | Errors, maybeValue?: string) { + setError(fieldOrFields: keyof TForm | Partial, maybeValue?: string) { this.setStore('errors', { ...this.errors, - ...((typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields) as Errors), + ...((typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields) as Partial), }) return this diff --git a/packages/vue3/src/useForm.ts b/packages/vue3/src/useForm.ts index 786cbeadb..7f42115d4 100644 --- a/packages/vue3/src/useForm.ts +++ b/packages/vue3/src/useForm.ts @@ -22,7 +22,7 @@ export interface InertiaFormProps { reset(...fields: (keyof TForm)[]): this clearErrors(...fields: (keyof TForm)[]): this setError(field: keyof TForm, value: string): this - setError(errors: Record): this + setError(errors: Partial>): this submit(method: Method, url: string, options?: FormOptions): void get(url: string, options?: FormOptions): void post(url: string, options?: FormOptions): void @@ -108,7 +108,7 @@ export default function useForm( return this }, - setError(fieldOrFields: keyof TForm | Record, maybeValue?: string) { + setError(fieldOrFields: keyof TForm | Partial>, maybeValue?: string) { Object.assign(this.errors, typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields) this.hasErrors = Object.keys(this.errors).length > 0