Skip to content

feat (form-core): Merging Form-level server errors with Field-level errors #1432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions examples/react/next-server-actions/src/app/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import { formOpts } from './shared-code'
const serverValidate = createServerValidate({
...formOpts,
onServerValidate: ({ value }) => {
if (value.age < 12) {
return 'Server validation: You must be at least 12 to sign up'
}
if (value.age < 12)
return { age: 'Server validation: You must be at least 12 to sign up' }
},
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { useActionState } from 'react'
import { mergeForm, useForm, useTransform } from '@tanstack/react-form'
import { initialFormState } from '@tanstack/react-form/nextjs'
import { useStore } from '@tanstack/react-store'
import someAction from './action'
import { formOpts } from './shared-code'

Expand All @@ -18,14 +17,8 @@ export const ClientComp = () => {
),
})

const formErrors = useStore(form.store, (formState) => formState.errors)

return (
<form action={action as never} onSubmit={() => form.handleSubmit()}>
{formErrors.map((error) => (
<p key={error as unknown as string}>{error}</p>
))}

<form.Field
name="age"
validators={{
Expand Down
1 change: 0 additions & 1 deletion examples/react/next-server-actions/src/app/shared-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { formOptions } from '@tanstack/react-form/nextjs'

export const formOpts = formOptions({
defaultValues: {
firstName: '',
age: 0,
},
})
40 changes: 40 additions & 0 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,31 @@
this.options.transform?.fn(newObj)
state = newObj.state
this.prevTransformArray = transformArray

const serverErrorMap = this.store.state.errorMap['onServer'] as any

batch(() => {

Check warning on line 1027 in packages/form-core/src/FormApi.ts

View check run for this annotation

Codecov / codecov/patch

packages/form-core/src/FormApi.ts#L1027

Added line #L1027 was not covered by tests
void (Object.values(this.fieldInfo) as FieldInfo<any>[]).forEach(
(field) => {
const fieldInstance = field.instance

Check warning on line 1030 in packages/form-core/src/FormApi.ts

View check run for this annotation

Codecov / codecov/patch

packages/form-core/src/FormApi.ts#L1030

Added line #L1030 was not covered by tests

if (!fieldInstance) return

if (fieldInstance.name in serverErrorMap) {
fieldInstance.setMeta((prev) => ({
...prev,
errorMap: {
...prev.errorMap,
onServer: serverErrorMap[fieldInstance.name],
},
}))
fieldInstance.mount()
}

this.validateField(fieldInstance.name, 'server')
},
)
})
}

return state
Expand Down Expand Up @@ -1388,6 +1413,20 @@
},
}))
}

/**
* when we have an error for onServer in the state, we want
* to clear the error as soon as the user changes the value in the field
*/
if (cause !== 'server') {
this.baseStore.setState((prev) => ({
...prev,
errorMap: {
...prev.errorMap,
onServer: undefined,
},
}))
}
})

return { hasErrored, fieldsErrorMap: currentValidationErrorMap }
Expand Down Expand Up @@ -1774,6 +1813,7 @@
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
...prev?.errorMap,
onMount: undefined,
onServer: undefined,
},
}))
}
Expand Down
15 changes: 14 additions & 1 deletion packages/react-form/src/nextjs/createServerValidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,25 @@
: onServerError
) as UnwrapFormAsyncValidateOrFn<TOnServer>

// Extract string values from errors if they're in object format
const errorsArray = onServerErrorVal
? Array.isArray(onServerErrorVal)
? onServerErrorVal.map((err) =>

Check warning on line 110 in packages/react-form/src/nextjs/createServerValidate.ts

View check run for this annotation

Codecov / codecov/patch

packages/react-form/src/nextjs/createServerValidate.ts#L109-L110

Added lines #L109 - L110 were not covered by tests
typeof err === 'object' ? Object.values(err)[0] : err,
)
: [

Check warning on line 113 in packages/react-form/src/nextjs/createServerValidate.ts

View check run for this annotation

Codecov / codecov/patch

packages/react-form/src/nextjs/createServerValidate.ts#L113

Added line #L113 was not covered by tests
typeof onServerErrorVal === 'object'
? Object.values(onServerErrorVal)[0]
: onServerErrorVal,

Check warning on line 116 in packages/react-form/src/nextjs/createServerValidate.ts

View check run for this annotation

Codecov / codecov/patch

packages/react-form/src/nextjs/createServerValidate.ts#L115-L116

Added lines #L115 - L116 were not covered by tests
]
: []

Check warning on line 118 in packages/react-form/src/nextjs/createServerValidate.ts

View check run for this annotation

Codecov / codecov/patch

packages/react-form/src/nextjs/createServerValidate.ts#L118

Added line #L118 was not covered by tests

const formState: ServerFormState<TFormData, TOnServer> = {
errorMap: {
onServer: onServerError,
},
values,
errors: onServerErrorVal ? [onServerErrorVal] : [],
errors: errorsArray,
}

throw new ServerValidateError({
Expand Down