shadcn/ui Form with React Hook Form and Zod v4: working setup
Build a validated shadcn/ui form with React Hook Form and Zod v4: working example, @hookform/resolvers v5, z.coerce types, and noValidate.
- shadcn 4.23.0
- Tailwind CSS 4.3.5
- Vite 8
- React 19.3
- react-hook-form 7.92.0
- zod 4.6.7
Building a validated form with shadcn/ui, React Hook Form and Zod v4 takes three packages and one resolver. Most errors after upgrading to Zod v4 come from an outdated @hookform/resolvers version or a type mismatch between z.input and z.output when using z.coerce or .default().
1. Install the packages
npm install react-hook-form zod @hookform/resolverspnpm add react-hook-form zod @hookform/resolversyarn add react-hook-form zod @hookform/resolversbun add react-hook-form zod @hookform/resolversnpx shadcn@latest add field input button2. Minimal working form
"use client"; import { useForm } from "react-hook-form";import { zodResolver } from "@hookform/resolvers/zod";import { z } from "zod";import { Button } from "@/components/ui/button";import { Input } from "@/components/ui/input"; const schema = z.object({ email: z.email("Enter a valid email address"), company: z.string().min(2, "At least 2 characters"),}); type FormValues = z.infer<typeof schema>; export function ContactForm() { const { register, handleSubmit, formState: { errors, isSubmitting } } = useForm<FormValues>({ resolver: zodResolver(schema), defaultValues: { email: "", company: "" }, }); const onSubmit = async (data: FormValues) => { await fetch("/api/contact", { method: "POST", body: JSON.stringify(data) }); }; return ( <form onSubmit={handleSubmit(onSubmit)} className="flex flex-col gap-4" noValidate> <div className="flex flex-col gap-1.5"> <label htmlFor="email" className="text-sm font-medium">Email</label> <Input id="email" type="email" aria-invalid={!!errors.email} {...register("email")} /> {errors.email && <p className="text-xs text-destructive">{errors.email.message}</p>} </div> <div className="flex flex-col gap-1.5"> <label htmlFor="company" className="text-sm font-medium">Company</label> <Input id="company" aria-invalid={!!errors.company} {...register("company")} /> {errors.company && <p className="text-xs text-destructive">{errors.company.message}</p>} </div> <Button type="submit" disabled={isSubmitting}>Send message</Button> </form> );}Three common Zod v4 + React Hook Form errors
- Type error on zodResolver(schema): Upgrade
@hookform/resolversto v5.1+ which supports Zod v4 natively. Older versions only type-check against Zod v3. - z.coerce.number() fails type inference: With
z.coerce, the input type isunknownwhile the output isnumber. PassuseForm<z.input<typeof schema>, unknown, z.output<typeof schema>>or useregister("age", { valueAsNumber: true })with plainz.number(). - Browser tooltip overrides Zod errors: Add
noValidateon<form>so the browser's built-in validation bubble doesn't hijack your custom error messages.
Questions
Do I need the shadcn Form wrapper or can I use Field + React Hook Form?
Both work. Modern shadcn/ui projects increasingly use the lighter Field / FieldLabel / FieldError primitives directly with register() or Controller, which keeps bundle size smaller and works identically with Radix and Base UI.
How do I show a toast after the form submits?
Call toast.success() from sonner inside onSubmit. If nothing appears, check shadcn/ui Sonner toast not showing.
Related articles
- shadcn/uishadcn/ui Sonner toast not showing: five causes and fixestoast() firing with nothing on screen? Check the five causes: missing <Toaster />, wrong import, next-themes in Vite/Astro, Server Components, and CSS tokens.
- shadcn/uishadcn/ui Button asChild not working: Slot errors and fixesFix React.Children.only and missing styles with shadcn/ui Button asChild: single child rule, fragments, forwarding props, and Base UI render prop.
- shadcn/uishadcn/ui Base UI vs Radix: which primitive library to pickCompare Radix UI (asChild, data-state) and Base UI (render prop, data-open) in shadcn/ui: composition, Tailwind v4 animations, registries, and when to pick each.
Still stuck? We answer every message ourselves. Write to us