Skip to content

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.

Updated 3 December 2026Tested with
  • 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/resolvers
Terminal
npx shadcn@latest add field input button

2. Minimal working form

contact-form.tsxTSX
"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/resolvers to 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 is unknown while the output is number. Pass useForm<z.input<typeof schema>, unknown, z.output<typeof schema>> or use register("age", { valueAsNumber: true }) with plain z.number().
  • Browser tooltip overrides Zod errors: Add noValidate on <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.

Was this page helpful?

Still stuck? We answer every message ourselves. Write to us

Building with shadcn/ui and Tailwind CSS v4?

Install 188 free blocks and components with the shadcn CLI (no account), preview them in your own brand in Studio, or get all 1,570 blocks and every theme in UI Pro (€199 once).

Use it with

This page is built from UI Pro parts: the docs article block (docs-quernly) with the code, terminal, install command, steps, contents and feedback components, included in UI Pro.