Skip to content

Commit fcfae79

Browse files
authored
feat(react-form): return parsed data from serverValidate function (#1318)
* feat(react-form): return parsed data from serverValidate function Change serverValidate return type from Promise<void> to Promise<TData> to provide direct access to validated and parsed data. Eliminates the need to decode formData twice, enhancing developer experience with immediate typed access to validation results. Closes #1317 * ci: apply automated fixes and generate docs * docs: Added example usecase of serverValidate as a comment
1 parent 6be8179 commit fcfae79

File tree

7 files changed

+45
-9
lines changed

7 files changed

+45
-9
lines changed

docs/framework/react/guides/ssr.md

+21-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ export const handleForm = createServerFn({
6767
})
6868
.handler(async (ctx) => {
6969
try {
70-
await serverValidate(ctx.data)
70+
const validatedData = await serverValidate(ctx.data)
71+
console.log('validatedData', validatedData)
72+
// Persist the form data to the database
73+
// await sql`
74+
// INSERT INTO users (name, email, password)
75+
// VALUES (${validatedData.name}, ${validatedData.email}, ${validatedData.password})
76+
// `
7177
} catch (e) {
7278
if (e instanceof ServerValidateError) {
7379
// Log form errors or do any other logic here
@@ -223,7 +229,13 @@ const serverValidate = createServerValidate({
223229

224230
export default async function someAction(prev: unknown, formData: FormData) {
225231
try {
226-
await serverValidate(formData)
232+
const validatedData = await serverValidate(formData)
233+
console.log('validatedData', validatedData)
234+
// Persist the form data to the database
235+
// await sql`
236+
// INSERT INTO users (name, email, password)
237+
// VALUES (${validatedData.name}, ${validatedData.email}, ${validatedData.password})
238+
// `
227239
} catch (e) {
228240
if (e instanceof ServerValidateError) {
229241
return e.formState
@@ -375,7 +387,13 @@ const serverValidate = createServerValidate({
375387
export async function action({ request }: ActionFunctionArgs) {
376388
const formData = await request.formData()
377389
try {
378-
await serverValidate(formData)
390+
const validatedData = await serverValidate(formData)
391+
console.log('validatedData', validatedData)
392+
// Persist the form data to the database
393+
// await sql`
394+
// INSERT INTO users (name, email, password)
395+
// VALUES (${validatedData.name}, ${validatedData.email}, ${validatedData.password})
396+
// `
379397
} catch (e) {
380398
if (e instanceof ServerValidateError) {
381399
return e.formState

examples/react/next-server-actions/src/app/action.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ const serverValidate = createServerValidate({
1717

1818
export default async function someAction(prev: unknown, formData: FormData) {
1919
try {
20-
await serverValidate(formData)
20+
const validatedData = await serverValidate(formData)
21+
console.log('validatedData', validatedData)
22+
// Persist the form data to the database
23+
// await sql`
24+
// INSERT INTO users (name, email, password)
25+
// VALUES (${validatedData.name}, ${validatedData.email}, ${validatedData.password})
26+
// `
2127
} catch (e) {
2228
if (e instanceof ServerValidateError) {
2329
return e.formState

examples/react/remix/app/routes/_index/route.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ const serverValidate = createServerValidate({
3030
export async function action({ request }: ActionFunctionArgs) {
3131
const formData = await request.formData()
3232
try {
33-
await serverValidate(formData)
33+
const validatedData = await serverValidate(formData)
34+
console.log('validatedData', validatedData)
35+
// Persist the form data to the database
36+
// await sql`
37+
// INSERT INTO users (name, email, password)
38+
// VALUES (${validatedData.name}, ${validatedData.email}, ${validatedData.password})
39+
// `
3440
} catch (e) {
3541
if (e instanceof ServerValidateError) {
3642
return e.formState

examples/react/tanstack-start/app/utils/form.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ export const handleForm = createServerFn({
2727
})
2828
.handler(async (ctx) => {
2929
try {
30-
await serverValidate(ctx.data)
30+
const validatedData = await serverValidate(ctx.data)
31+
console.log('validatedData', validatedData)
32+
// Persist the form data to the database
33+
// await sql`
34+
// INSERT INTO users (name, email, password)
35+
// VALUES (${validatedData.name}, ${validatedData.email}, ${validatedData.password})
36+
// `
3137
} catch (e) {
3238
if (e instanceof ServerValidateError) {
3339
// Log form errors or do any other logic here

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export const createServerValidate =
9696
validationSource: 'form',
9797
})) as UnwrapFormAsyncValidateOrFn<TOnServer> | undefined
9898

99-
if (!onServerError) return
99+
if (!onServerError) return values
100100

101101
const onServerErrorVal = (
102102
isGlobalFormValidationError(onServerError)

packages/react-form/src/remix/createServerValidate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export const createServerValidate =
9696
validationSource: 'form',
9797
})) as UnwrapFormAsyncValidateOrFn<TOnServer> | undefined
9898

99-
if (!onServerError) return
99+
if (!onServerError) return values
100100

101101
const onServerErrorVal = (
102102
isGlobalFormValidationError(onServerError)

packages/react-form/src/start/createServerValidate.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export const createServerValidate =
100100
validationSource: 'form',
101101
})) as UnwrapFormAsyncValidateOrFn<TOnServer> | undefined
102102

103-
if (!onServerError) return
103+
if (!onServerError) return data
104104

105105
const onServerErrorVal = (
106106
isGlobalFormValidationError(onServerError)

0 commit comments

Comments
 (0)