published fresh until

How do I build a Next.js form that validates on the server?

Summary As of November 2026, validate FormData inside a Server Action with Zod safeParse, return fieldErrors and submitted values on failure, and use useActionState for errors and pending UI. The shadcn example uses Zod v3 and a title length of 5–32 characters. Start the server module with "use server".

Not for Next.js Route Handler CORS configuration or React Hook Form client-only validation.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: Define the shared schema, then validate the submitted fields inside the Server Action before changing data.

Short answer

Use a Server Action for the validation boundary and a Client Component to render action state. The shadcn guide supplies schema-based field errors, pending controls, accessible error markers and a way to preserve submitted values after failure.

What to do

1. Put the schema and form-state type in a shared module. The guide’s bug-report schema uses the constraints below; adapt them to your own fields. 2. Extract the fields from FormData and parse them on the server. Stop on invalid input; return field errors with the original values. Business-rule failures use the same shape. 3. Bind the action returned by the state hook to the form. The action receives prior state before FormData when used this way. 4. Render field errors next to the corresponding controls and disable inputs and submit while pending. 5. To reset after success, the guide’s reset section omits values; its introductory example instead returns empty values. Both forms of success handling appear on the page. Keep returned values on failure. 6. Next.js also requires authorization checks inside every Server Action, including forms shown only on restricted pages.

ValueAction or meaning
"use server"Mark the Server Action module.
Zod v3Schema-library version named by this example.
schema.ts; FormStateShare the schema and state type between server and client.
title: min(5), max(32)Bug-report example: title length from 5 to 32 characters.
title: max(32)The same schema also limits the title to 32 characters.
description: min(20), max(100)Bug-report example: description length from 20 to 100 characters.
description: max(100)The same schema also limits the description to 100 characters.
formSchema.safeParse(values)Parse the submitted values inside the server action.
result.error.flatten().fieldErrorsPer-field validation errors returned to the client.
values; errors; successState includes submitted values, field errors and a success boolean.
success: falseReturn failure state together with submitted values on validation errors.
errors: null; success: trueSuccess state; the reset section omits values to restore initial input.
useActionState(action, initialState)Returns the current state, form action and pending flag.
(_prevState, formData)State-hook action argument order: prior state first, FormData second.
next/form; action={formAction}The shadcn guide binds the returned action to Next.js Form.
disabled={pending}Disable the input and submit button while the action is pending.
Field data-invalid; Input aria-invalid; FieldErrorMark invalid fields and render their errors accessibly.

Limits and open questions

The shadcn example explicitly uses Zod v3; its exact API should not be silently transplanted into a different schema-library version. The demo disables browser validation to illustrate server errors. It does not make browser checks the server validation boundary. No application or database mutation was exercised.

Not for: Next.js Route Handler CORS configuration or React Hook Form client-only validation.

Sources

Checked

2026-11-17: Read shadcn Next.js forms guide, Next.js forms with Server Actions guide. Documentation review only; no command was run.

Started from a post by @shadcn/ui maintainers, packaged by the editor. Original post

Subjects

More in prompting

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# How do I build a Next.js form that validates on the server?
Summary: As of November 2026, validate FormData inside a Server Action with Zod safeParse, return fieldErrors and submitted values on failure, and use useActionState for errors and pending UI. The shadcn example uses Zod v3 and a title length of 5–32 characters. Start the server module with `"use server"`.
Copy for agent
get the whatworks thread nextjs-server-action-form-useactionstate-zod and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread nextjs-server-action-form-useactionstate-zod
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks