shadcn form with React Hook Form + Zod
A shadcn form with React Hook Form and Zod has four parts: a Zod schema, useForm with zodResolver(schema), inputs in Controllers inside shadcn’s Field, and form.handleSubmit. Parse the schema again on the server. mebius.design’s free forms skip React Hook Form but can use the same schema.
8 min read · Updated Nov 22, 2026 · React + Tailwind
What do you need for a shadcn form with React Hook Form and Zod?
Versions on npm and in each project’s docs, checked on 22 Nov 2026.
Four shadcn components and three npm packages. shadcn/ui’s React Hook Form guide builds each field from its Field component, React Hook Form keeps the state, Zod holds the rules, and @hookform/resolvers connects the two.
npx shadcn@latest add field input button checkbox
npm install react-hook-form zod @hookform/resolvers| react-hook-form | 7.92.0 (11 Nov 2026), MIT, for React 16.8 to 19 |
|---|---|
| zod | 4.6.7 (12 Oct 2026), MIT. shadcn/ui’s guide says its example uses Zod v3 |
| @hookform/resolvers | 7.0.8 (20 Nov 2026), MIT, for Zod 3.25+ or 4 and React Hook Form 7.55+ |
| shadcn CLI | 4.23.0, MIT. Field, FieldLabel, FieldError and FieldGroup come from the field item |
| mebius.design forms | 128 free forms in light and dark, on plain React state |
| Checked on | 22 Nov 2026; every sample below compiled with these versions |
How do you write the Zod schema for a shadcn form?
One schema for the rules, the messages and the type.
Put the schema in its own file, so the form and the server import the same rules. Zod 4’s docs import it as a namespace, write emails as z.email() and pass messages as { error } (checked on 22 Nov 2026):
import * as z from "zod"
// One schema: the rules, the messages and the type. The form and the server both read it.
export const signUpSchema = z.object({
firstName: z.string().trim().min(1, { error: "Required" }),
lastName: z.string().trim().min(1, { error: "Required" }),
email: z.email({ error: "Enter a valid email" }),
password: z.string().min(8, { error: "Use 8+ characters" }),
agreedToTerms: z.boolean().refine((agreed) => agreed, { error: "Agree to continue" }),
})
export type SignUpValues = z.infer<typeof signUpSchema>- z.infer turns the schema into the form’s TypeScript type, so the fields and the rules can’t drift apart.
- .trim() runs before .min(1), so a name made of spaces fails.
- A terms checkbox is a boolean with a refine. With z.literal(true), TypeScript rejects the starting value false in defaultValues.
- Short messages such as “Required” fit a shadcn FieldError and the mebius.design form further down, which shows them beside the label.
How do you connect a Zod schema to React Hook Form’s useForm?
From the React Hook Form v7 docs, checked on 22 Nov 2026.
Pass the resolver
resolver: zodResolver(signUpSchema)lets Zod run every check. React Hook Form’s useForm docs say a resolver can’t be combined with built-in rules such as required.Give every field a default
shadcn/ui’s guide sets defaultValues for every field, empty strings included, so each input is controlled from the first render.
Pick a mode
The mode option sets when checks start: onSubmit (the default), onBlur, onChange, onTouched or all. onTouched checks a field when it loses focus, then on every change. After a submit, reValidateMode (onChange by default) re-checks as people type.
Submit through handleSubmit
form.handleSubmit(onSubmit) validates first, so onSubmit only gets valid, typed values. shouldFocusError is on by default, so a failed submit focuses the first invalid field.
How do you render React Hook Form fields with shadcn’s Field component?
The Controller pattern from shadcn/ui’s guide, written with Zod 4.
Each input sits in a Controller. Its render function hands you field (the value, onChange, onBlur and ref) and fieldState (invalid and the error). Field takes data-invalid, the input takes aria-invalid, and FieldError prints the message:
"use client"
import { zodResolver } from "@hookform/resolvers/zod"
import { Controller, useForm } from "react-hook-form"
import { signUp } from "@/app/sign-up/actions"
import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import { Field, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
import { signUpSchema, type SignUpValues } from "@/lib/sign-up-schema"
const fields = [
{ name: "firstName", label: "First name", type: "text", autoComplete: "given-name" },
{ name: "lastName", label: "Last name", type: "text", autoComplete: "family-name" },
{ name: "email", label: "Email", type: "email", autoComplete: "email" },
{ name: "password", label: "Password", type: "password", autoComplete: "new-password" },
] as const
export function SignUpForm({ onDone }: { onDone: () => void }) {
const form = useForm<SignUpValues>({
resolver: zodResolver(signUpSchema),
defaultValues: { firstName: "", lastName: "", email: "", password: "", agreedToTerms: false },
mode: "onTouched",
})
async function onSubmit(values: SignUpValues) {
const result = await signUp(values)
if (result.ok) onDone()
else form.setError(result.field, { message: result.message })
}
return (
<form onSubmit={form.handleSubmit(onSubmit)} noValidate>
<FieldGroup>
{fields.map(({ name, label, type, autoComplete }) => (
<Controller
key={name}
name={name}
control={form.control}
render={({ field, fieldState }) => (
<Field data-invalid={fieldState.invalid}>
<FieldLabel htmlFor={`sign-up-${name}`}>{label}</FieldLabel>
<Input {...field} id={`sign-up-${name}`} type={type} autoComplete={autoComplete} aria-invalid={fieldState.invalid} />
{fieldState.invalid && <FieldError errors={[fieldState.error]} />}
</Field>
)}
/>
))}
<Controller
name="agreedToTerms"
control={form.control}
render={({ field, fieldState }) => (
<Field orientation="horizontal" data-invalid={fieldState.invalid}>
<Checkbox
id="sign-up-terms"
checked={field.value}
onCheckedChange={(checked) => field.onChange(checked === true)}
onBlur={field.onBlur}
aria-invalid={fieldState.invalid}
/>
<FieldLabel htmlFor="sign-up-terms">I agree to the terms</FieldLabel>
{fieldState.invalid && <FieldError errors={[fieldState.error]} />}
</Field>
)}
/>
{form.formState.errors.root && <FieldError>{form.formState.errors.root.message}</FieldError>}
<Button type="submit" disabled={form.formState.isSubmitting}>
{form.formState.isSubmitting ? "Creating account…" : "Create account"}
</Button>
</FieldGroup>
</form>
)
}- Text inputs take {...field}. Checkboxes, selects and switches take field.value and field.onChange instead, as the checkbox does here.
- noValidate hides the browser’s own error bubbles so Zod’s messages show. shadcn/ui’s guide turns browser validation off for its demo too, and recommends keeping basic browser validation in production.
- formState.isSubmitting covers the wait for the server, and setError puts the server’s answer on a field or on root.
How do you validate a shadcn form on the server with the same Zod schema?
A Next.js Server Function that parses its input again.
Client checks help the person typing but don’t protect the server. The Next.js docs on mutating data point out that Server Functions also answer direct POST requests, so the action parses its input with the same schema before it trusts it (checked on 22 Nov 2026):
"use server"
import { signUpSchema } from "@/lib/sign-up-schema"
export type SignUpResult = { ok: true } | { ok: false; field: "email" | "root"; message: string }
export async function signUp(input: unknown): Promise<SignUpResult> {
// A Server Function also takes direct POST requests, so the input is parsed again here.
const parsed = signUpSchema.safeParse(input)
if (!parsed.success) return { ok: false, field: "root", message: "Check the form and try again." }
// parsed.data is typed and checked: create the user with your auth provider here.
// If the email is taken: return { ok: false, field: "email", message: "That email already has an account" }
return { ok: true }
}safeParse never throws: it returns success with the typed data, or the issues with their paths and messages. mebius.design covers the UI, the Figma file and the React front end; accounts, sessions and email stay with your auth provider.
Do mebius.design’s free forms use React Hook Form or Zod?
Read from the Forms and Forms Pro registry source on 22 Nov 2026.
No. None of the 128 free mebius.design forms imports react-hook-form or zod. Each one is a single file that keeps its values in React state, checks its fields in its own code, and calls your onSubmit with valid values. While that promise is pending the button shows progress; if it rejects, the form keeps everything typed and shows an error line.
So a mebius.design form doesn’t go inside useForm. It takes your Zod schema in one of two places: on the server, or in place of its own checks. The example is the Create Account form, a sign-up card with a password meter, a country select and a terms checkbox. Install the kit theme first, then the form:
npx shadcn@latest add @mebius/forms-theme
npx shadcn@latest add @mebius/forms-create-account --overwriteCreate Account needs a free account and the @mebius registry in components.json (setup guide); the open starter forms install from their URL with no account.
How do you check a mebius.design form’s values with a Zod schema?
The schema on the server, the form unchanged.
Extend the sign-up schema with the Create Account form’s country, parse it in a Server Function, and throw in onSubmit when the server says no:
import * as z from "zod"
import { signUpSchema } from "@/lib/sign-up-schema"
// The countries the form offers: pass the same codes to the form's `countries` prop.
export const countryCodes = ["DE", "NL", "GB"] as const
// The mebius Create Account form's values: the sign-up fields plus a country.
export const createAccountSchema = signUpSchema.extend({
country: z.enum(countryCodes, { error: "Choose a country" }),
})"use server"
import { createAccountSchema } from "@/lib/create-account-schema"
export async function createAccount(input: unknown): Promise<string | null> {
const parsed = createAccountSchema.safeParse(input)
if (!parsed.success) return parsed.error.issues[0]?.message ?? "Check the form"
// parsed.data is typed and checked: create the user with your auth provider here.
return null
}"use client"
import { createAccount } from "@/app/join/actions"
import { CreateAccountForm, type CreateAccountCountry, type CreateAccountFormValues } from "@/components/create-account-form"
const countries: CreateAccountCountry[] = [
{ code: "DE", name: "Germany", dialCode: "+49" },
{ code: "NL", name: "Netherlands", dialCode: "+31" },
{ code: "GB", name: "United Kingdom", dialCode: "+44" },
]
export function JoinForm() {
async function submit(values: CreateAccountFormValues) {
const problem = await createAccount(values)
// A throw keeps every field filled in and shows the form's own error line.
if (problem) throw new Error(problem)
}
return <CreateAccountForm countries={countries} defaultValues={{ country: "DE" }} termsHref="/terms" onSubmit={submit} />
}The Create Account form shows its own line on a throw, “Couldn’t create your account. Try again.”, not the server’s message, so log the message or show it next to the form if people need the detail.
Can a mebius.design form run your Zod schema in the browser?
Yes: edit the validate function in the installed file.
The shadcn CLI copies the form’s source into your project, so its checks are yours to change. In components/create-account-form.tsx, validate returns { field: message } for the fields it is asked about. Build that object from Zod’s issues instead, and the Create Account form keeps the rest: messages beside the labels, focus on the first invalid field, the shake, and errors that clear as each field is fixed.
// at the top of the file
import { createAccountSchema } from "@/lib/create-account-schema"
// inside CreateAccountForm, in place of the form's own validate()
function validate(v: CreateAccountFormValues, only?: FieldName): FieldErrors {
const found: FieldErrors = {}
const result = createAccountSchema.safeParse(v)
if (result.success) return found
for (const issue of result.error.issues) {
const field = issue.path[0] as FieldName
if ((!only || only === field) && !found[field]) found[field] = issue.message
}
return found
}- Keep the schema’s country codes and password length equal to the countries and minPasswordLength props: the schema now decides what is valid.
- Reinstalling the form with --overwrite replaces the edit, as with any changed shadcn component.
Should you build a shadcn form with React Hook Form or start from a mebius.design form?
It depends on whose design the form should have.
- React Hook Form, Zod and shadcn’s Field: forms in your own shadcn theme, field arrays (useFieldArray), fields that come and go, and one pattern across many forms.
- A mebius.design form: a finished design with its Figma file, light and dark, anime.js motion, and the submit, error and success states done. Your Zod schema still decides what is valid.
For forms over several steps, see the multi-step form guide; for login, checkout, booking and survey forms, see shadcn form examples. All 128 forms have a live demo on the free shadcn forms page.
FAQ
Answers to what people ask most.
Does shadcn/ui still use the Form component with React Hook Form?
Not in its current React Hook Form guide, which builds fields with Field, FieldLabel and FieldError inside React Hook Form’s Controller. shadcn/ui’s forms docs list React Hook Form, TanStack Form and Formisch, with useActionState marked as coming soon (checked on 22 Nov 2026).
Does zodResolver work with Zod 4?
Yes. @hookform/resolvers 7.0.8 supports Zod 3.25 or later and Zod 4, with React Hook Form 7.55 or later (npm, checked on 22 Nov 2026). shadcn/ui’s guide says its example uses Zod v3; the samples on this page use Zod 4.6.7 and compile.
Can I put a mebius.design form inside a React Hook Form form?
Not as nested fields. Each mebius.design form renders its own form element, state and checks, and HTML doesn’t allow a form inside a form. Use it as a whole form and give its values to your Zod schema, on the server or in the form’s own checks.
Are the mebius.design forms free?
Yes. All 128 are free. The open starter forms (among mebius.design’s 20 starter items) are MIT and install from their URL with no account; the rest need a free mebius.design account. Each comes in light and dark, with its Figma file.
Related
Item pages, kits and guides this guide uses.
- Free forms: 128 free React + Tailwind forms in light and dark, with Figma files, for the shadcn CLI.
- shadcn form examples: Login, sign-up, checkout, settings, booking and survey forms by use case.
- Multi-step form: Validation per step in plain React, then two free multi-step forms.