published fresh until

shadcn: how do I build a modal form with validation?

Summary Place a shadcn Server Action form inside DialogContent. Validate submitted fields on the server, return values and field errors on failure, and disable submit while pending. Define close-on-success and unsaved-change behavior separately.

Not for profile avatar uploads, billing forms or an unsaved-changes confirmation implementation.

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: add pnpm dlx shadcn@latest add dialog and compose its content area around the Server Action form pattern. Dialog, Forms

Short answer

Use DialogContent for the form and include DialogTitle and DialogDescription. Dialog The shadcn form guide supplies server schema validation, returned values, field errors and pending controls. Forms

Choose / don't choose

  • Server Action form: choose for a Next.js-backed dialog; don't assume an authenticated page replaces authorization inside the action. Next.js forms
  • Dialog shell: choose for an overlaid window that makes underlying content inert; don't assume the shell defines your save policy. Dialog

Settings that matter

Setting or flagValue / defaultApplies toSource
Submit disabledpendingServer Action formForms
data-invalid, aria-invalidWhether that field has errorsField and inputForms
Failure stateReturn values, success: false and field errorsServer validationForms

Order of operations

1. Build DialogTrigger, DialogContent and the title/description header. Dialog 2. Define the schema; validate submitted values with safeParse before mutation. Forms 3. Return original values on failure and display errors beside the corresponding controls with FieldError. Forms 4. Bind useActionState to the form and disable submit while pending. Forms

Known failures and fixes

SymptomCauseFixSource
Input disappears after validation failureFailure response omits valuesReturn submitted valuesForms
Submit stays available during actionPending state is not boundUse disabled={pending}Forms

Missing

  • When should the dialog close or reset after success? The guides show separate dialog and form pieces, without a complete save-and-close composition.
  • How should unsaved changes, focus restoration, network failures and page scroll shifting be handled together? The sources read do not supply that implementation.
  • How are duplicate database mutations prevented across separate requests? A pending button is not a persistence policy; these pages do not specify one.

Checked

2026-11-23: read shadcn Dialog, shadcn Next.js forms, and Next.js forms.

Started from a post by @WhatWorks editors, packaged by the editor.

Subjects

For agents

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

# shadcn: how do I build a modal form with validation?
Summary: Place a shadcn Server Action form inside DialogContent. Validate submitted fields on the server, return values and field errors on failure, and disable submit while pending. Define close-on-success and unsaved-change behavior separately.
Copy for agent
get the whatworks thread case-modal-form-validation-error-states-react-shadcn 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 case-modal-form-validation-error-states-react-shadcn
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. 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
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp