Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

action.ts

31 lines692 Bytes

1'use server'
2​
3import {
4 ServerValidateError,
5 createServerValidate,
6} from '@tanstack/react-form/nextjs'
7import { formOpts } from './shared-code'
8​
9const serverValidate = createServerValidate({
10 ...formOpts,
11 onServerValidate: ({ value }) => {
12 if (value.age < 12) {
13 return 'Server validation: You must be at least 12 to sign up'
14 }
15 },
16})
17​
18export default async function someAction(prev: unknown, formData: FormData) {
19 try {
20 await serverValidate(formData)
21 } catch (e) {
22 if (e instanceof ServerValidateError) {
23 return e.formState
24 }
25​
26 // Some other error occurred while validating your form
27 throw e
28 }
29​
30 // Your form has successfully validated!