Zod 21 🛡️ shadcn/ui Forms — FormField, Controller, and the Field Component Migration
shadcn/ui has two form layers, and they are not interchangeable within a single form. The legacy Form composition (FormField, FormItem, FormControl, FormLabel, FormMessage) wraps React Hook Form’s FormProvider and Controller into a seven-primitive tree that handles accessibility wiring automatically . The new Field family (Field, FieldLabel, FieldError, FieldGroup) is a standalone accessibility foundation that works with React Hook Form, TanStack Form, or no form library at all . The migration is not a drop-in replacement; it is a decision about which layer a form belongs to, and the two must never be mixed in the same <form> tree because they do not share id contexts or aria wiring .
This chapter covers both layers, the mapping between them, and the migration path that moves a form from the legacy composition to the Field primitives with Controller. The sections that follow walk through the legacy FormField pattern, the Field + Controller pattern, and the component mapping that translates one to the other.
Key point: The legacy FormField composition is RHF-coupled and still works; the new Field primitives are library-agnostic and recommended for new code; use Controller with Field for controlled components, and never mix FormField and Field inside the same form.
a. The legacy FormField composition
The legacy layer wraps React Hook Form in a set of primitives that supply label, control, description, and error message around a shadcn/ui input. The Form component provides the RHF context; FormField is a render-prop wrapper around Controller; FormItem is the layout container; FormControl uses a Radix Slot to inject id, aria-describedby, and aria-invalid onto the input; FormLabel and FormMessage read from the field context .
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import {
Form, FormControl, FormField, FormItem, FormLabel, FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
const Schema = z.object({
username: z.string().min(2, "Enter at least two characters"),
});
function ProfileForm() {
const form = useForm<z.infer<typeof Schema>>({
resolver: zodResolver(Schema),
defaultValues: { username: "" },
});
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(console.log)}>
<FormField
control={form.control}
name="username"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</form>
</Form>
);
}
The FormControl wrapper is what makes the accessibility work: it clones the child and passes the id from FormItem, the aria-invalid from the field state, and the aria-describedby pointing at the message element . Without it, the label’s htmlFor has nothing to point at and the error message is not announced. The FormMessage component reads errors[name] from the context and renders the message when present.
This layer is still available in the registry (npx shadcn@latest add form) and is not being removed, but it is no longer actively developed . A codebase already using it can keep using it; the migration is for new forms or forms being rewritten.
b. The Field + Controller pattern
The new Field primitives are not a form library. They are a set of components that render a label, a control, a description, and an error message with the correct htmlFor, id, and aria-* attributes, and they leave the state management to whatever library the application uses . With React Hook Form, the control is wrapped in Controller, and the field and fieldState props are spread onto the primitives.
import { Controller, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import { Field, FieldError, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
const Schema = z.object({
email: z.email("Enter a valid email address"),
});
function EmailForm() {
const form = useForm<z.infer<typeof Schema>>({
resolver: zodResolver(Schema),
defaultValues: { email: "" },
});
return (
<form onSubmit={form.handleSubmit(console.log)}>
<Controller
control={form.control}
name="email"
render={({ field, fieldState }) => (
<Field data-invalid={fieldState.invalid || undefined}>
<FieldLabel htmlFor={field.name}>Email</FieldLabel>
<Input {...field} id={field.name} aria-invalid={fieldState.invalid} />
{fieldState.invalid && <FieldError errors={[fieldState.error]} />}
</Field>
)}
/>
</form>
);
}
The data-invalid attribute on Field is what styles the entire block in the error state, and the aria-invalid on the input is what assistive technology announces . The FieldError component accepts an errors array—[fieldState.error]—and renders the message with the correct role="alert" and association. The htmlFor on FieldLabel must match the id on the input; passing id={field.name} is the simplest way to ensure that.
For a native input that accepts register, the Controller is unnecessary. The Field primitives wrap the input directly, and the register return is spread onto the control.
const { register, formState: { errors } } = useForm();
<Field data-invalid={!!errors.username || undefined}>
<FieldLabel htmlFor="username">Username</FieldLabel>
<Input id="username" {...register("username")} aria-invalid={!!errors.username} />
{errors.username && <FieldError errors={[errors.username]} />}
</Field>
The difference is that Controller is required for controlled components like Select, DatePicker, and Switch, which do not expose a native ref that register can attach to . The Field layer does not change that requirement; it only changes what surrounds the control.
c. The migration mapping
The migration from the legacy composition to the Field primitives is a structural change, not a rename. The FormField wrapper disappears, and its render prop moves inside the Field component. The FormControl wrapper disappears entirely, because the Field primitives do not clone children; the control receives its id and aria-invalid directly. The mapping is documented in the migration guides and the shadcn/ui Field reference .
| Legacy | New | Notes |
|---|---|---|
FormField | Controller | Render prop moves inside |
FormItem | Field | Container |
FormControl | (removed) | Control receives props directly |
FormLabel | FieldLabel | Pass htmlFor matching input id |
FormMessage | FieldError | Takes errors={[fieldState.error]} |
FormDescription | FieldDescription | Helper text |
Form | (not required) | FormProvider still available |
The migration is per-form, not per-component. A codebase can have both form.tsx and field.tsx installed and use the legacy layer in one form and the Field layer in another; what it cannot do is mix the two inside a single <form> tree . The reason is the id context: FormControl generates an id with useId() and passes it to FormLabel and FormMessage through context; Field expects the developer to pass the id explicitly. Mixing them means one layer’s htmlFor points at an id the other layer never set.
The FieldError component also accepts Standard Schema issues directly, which means the Zod issues array can be passed without mapping to fieldState.error . This is useful when the form displays a server-side error that is not a field-level validation failure.
Complete Example Session
// ============================================
// PART 1: LEGACY FORM (RHF-COUPLED)
// ============================================
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form";
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
<FormField
control={form.control}
name="username"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl><Input {...field} /></FormControl>
<FormMessage />
</FormItem>
)}
/>
</form>
</Form>
// ============================================
// PART 2: FIELD + CONTROLLER (LIBRARY-AGNOSTIC)
// ============================================
import { Controller } from "react-hook-form";
import { Field, FieldError, FieldLabel } from "@/components/ui/field";
<Controller
control={form.control}
name="username"
render={({ field, fieldState }) => (
<Field data-invalid={fieldState.invalid || undefined}>
<FieldLabel htmlFor={field.name}>Username</FieldLabel>
<Input {...field} id={field.name} aria-invalid={fieldState.invalid} />
{fieldState.invalid && <FieldError errors={[fieldState.error]} />}
</Field>
)}
/>
// ============================================
// PART 3: NATIVE INPUT (NO CONTROLLER)
// ============================================
<Field data-invalid={!!errors.email || undefined}>
<FieldLabel htmlFor="email">Email</FieldLabel>
<Input id="email" {...register("email")} aria-invalid={!!errors.email} />
{errors.email && <FieldError errors={[errors.email]} />}
</Field>
// ============================================
// PART 4: THE MIXING RULE
// ============================================
// DO NOT mix FormField and Field in the same <form>.
// FormControl uses a context-generated id; Field expects an explicit id.
// Mixing breaks htmlFor/aria-describedby wiring.
The parts covered the legacy composition, the Field + Controller pattern, the native-input Field pattern, and the mixing rule.
Quick Reference
Component Mapping
| Legacy | New | Purpose |
|---|---|---|
Form | — | RHF context (still available) |
FormField | Controller | Render prop wrapper |
FormItem | Field | Field container |
FormControl | — | Removed; props direct |
FormLabel | FieldLabel | Label with htmlFor |
FormMessage | FieldError | Error display |
FormDescription | FieldDescription | Helper text |
Field Primitives
| Component | Purpose |
|---|---|
Field | Core wrapper, data-invalid |
FieldLabel | Label with htmlFor |
FieldError | Error container, errors array |
FieldDescription | Helper text |
FieldGroup | Stack related fields |
FieldSet / FieldLegend | Semantic grouping |
Installation
| Layer | Command |
|---|---|
| Legacy | npx shadcn@latest add form |
| New | npx shadcn@latest add field |
| Both | npx shadcn@latest add field form |
Best Practices
✅ Do This:
// Use Field + Controller for new forms
<Controller control={control} name="x" render={({ field, fieldState }) => (
<Field data-invalid={fieldState.invalid}>
<FieldLabel htmlFor={field.name}>X</FieldLabel>
<Input {...field} id={field.name} aria-invalid={fieldState.invalid} />
{fieldState.invalid && <FieldError errors={[fieldState.error]} />}
</Field>
)} /> // ✅
// Pass the id explicitly
<FieldLabel htmlFor="email">Email</FieldLabel>
<Input id="email" {...register("email")} /> // ✅
// Use FieldError with Standard Schema issues
<FieldError errors={result.error.issues} /> // ✅
❌ Don’t Do This:
// Mix FormField and Field in one form
<Form><Field>...</Field></Form> // breaks aria wiring // ❌
// Assume FormControl is needed with Field
<Field><FormControl><Input /></FormControl></Field> // ❌
// Forget the id on the input
<FieldLabel htmlFor="email">Email</FieldLabel>
<Input {...register("email")} /> // no id, label points nowhere // ❌
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Label not associated | htmlFor does not match input id | Pass id={field.name} |
| Error not announced | Missing aria-invalid on input | Add aria-invalid={fieldState.invalid} |
| Mixing breaks wiring | FormControl id context not shared with Field | Use one layer per form |
Controller not needed | Native input accepts register | Use register directly |
FieldError renders nothing | Passed fieldState.error directly | Wrap in array: errors={[fieldState.error]} |
Real-World Examples
1. Legacy FormField
<FormField control={form.control} name="x" render={({ field }) => (
<FormItem><FormLabel>X</FormLabel><FormControl><Input {...field} /></FormControl><FormMessage /></FormItem>
)} />
2. Field + Controller
<Controller control={control} name="x" render={({ field, fieldState }) => (
<Field data-invalid={fieldState.invalid}><FieldLabel htmlFor={field.name}>X</FieldLabel><Input {...field} id={field.name} /><FieldError errors={[fieldState.error]} /></Field>
)} />
3. Native input with Field
<Field><FieldLabel htmlFor="email">Email</FieldLabel><Input id="email" {...register("email")} /><FieldError errors={[errors.email]} /></Field>
4. FieldGroup
<FieldGroup><Field>...</Field><Field>...</Field></FieldGroup>
5. FieldSet + FieldLegend
<FieldSet><FieldLegend>Profile</FieldLegend><FieldGroup>...</FieldGroup></FieldSet>
6. FieldError with issues
<FieldError errors={result.error.issues} />
7. data-invalid styling
<Field data-invalid={fieldState.invalid || undefined}>
8. Installation
npx shadcn@latest add field form
9. "use client" requirement
"use client"; // Field + Controller needs client rendering
10. Mixing rule
One layer per form; never FormField and Field in the same tree.
Visual
Legacy vs New Layer
┌─────────────────────────────────────────────────────────┐
│ LEGACY (Form composition) │
│ ┌──────────────────────────────────────────┐ │
│ │ Form (RHF context) │ │
│ │ FormField → Controller │ │
│ │ FormItem │ │
│ │ FormLabel │ │
│ │ FormControl (clones child, id) │ │
│ │ FormMessage │ │
│ └──────────────────────────────────────────┘ │
│ RHF-coupled, id via context │
└─────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────┐
│ NEW (Field primitives) │
│ ┌──────────────────────────────────────────┐ │
│ │ Controller (RHF) │ │
│ │ Field (data-invalid) │ │
│ │ FieldLabel (htmlFor) │ │
│ │ Input (id, aria-invalid) │ │
│ │ FieldError (errors array) │ │
│ └──────────────────────────────────────────┘ │
│ Library-agnostic, id explicit │
└─────────────────────────────────────────────────────────┘
Migration Decision
┌─────────────────────────────────────────────────────────┐
│ NEW FORM? │
│ │ │
│ ┌────┴────┐ │
│ ▼ ▼ │
│ YES NO │
│ │ │ │
│ ▼ ▼ │
│ Use Field Already on Form? │
│ + Controller │ │
│ ┌───┴───┐ │
│ ▼ ▼ │
│ YES NO │
│ │ │ │
│ ▼ ▼ │
│ Keep it Use Field │
│ (works) (new) │
└─────────────────────────────────────────────────────────┘
The Mixing Rule
┌─────────────────────────────────────────────────────────┐
│ FORM A FORM B │
│ ┌──────────────────┐ ┌──────────────────┐ │
│ │ FormField │ │ Field │ │
│ │ FormControl │ │ Controller │ │
│ │ (id via context) │ │ (id explicit) │ │
│ └──────────────────┘ └──────────────────┘ │
│ │
│ OK: different forms use different layers │
│ │
│ FORM C │
│ ┌──────────────────────────────────────────┐ │
│ │ FormField + Field in same tree │ │
│ │ → id contexts do not match │ │
│ │ → aria wiring breaks │ │
│ └──────────────────────────────────────────┘ │
│ NOT OK │
└─────────────────────────────────────────────────────────┘
Summary
| Item | Value |
|---|---|
| Legacy layer | Form, FormField, FormItem, FormControl, FormLabel, FormMessage |
| New layer | Field, FieldLabel, FieldError, FieldGroup, FieldSet |
| Legacy coupling | RHF via FormProvider + Controller |
| New coupling | None; works with RHF, TanStack, or no library |
| Migration mapping | FormField → Controller, FormItem → Field, FormMessage → FieldError |
FormControl | Removed in new layer; props passed directly |
| Mixing rule | Never FormField and Field in the same form |
| Installation | npx shadcn@latest add field or add form |
Key takeaways:
- shadcn/ui has two form layers, and they are not interchangeable. The legacy
Formcomposition is RHF-coupled and still works; the newFieldprimitives are library-agnostic and are the recommended path for new code . FormControlis what makes the legacy layer accessible. It clones the child and injects theidfrom context, thearia-invalid, and thearia-describedby. The newFieldlayer does not clone children; the developer passesidandaria-invalidexplicitly .Controlleris required for controlled components in both layers. A native input that acceptsregisterdoes not need it, but aSelect,DatePicker, orSwitchdoes, because those components do not expose a native ref .- The migration is a mapping, not a rename.
FormFieldbecomesController,FormItembecomesField,FormMessagebecomesFieldError, andFormControldisappears . - Never mix the two layers in one form. The
idcontexts do not match, and the aria wiring breaks. A codebase can have both installed and use one layer per form, but not both in the same<form>tree .
Remember: The Field migration is not a syntax update; it is a shift from a form-library-coupled composition to a set of standalone accessibility primitives. The legacy layer still works and is not being removed, but new forms should use Field with Controller for controlled inputs and register for native ones. The htmlFor on the label must match the id on the input, the aria-invalid must be on the control, and the FieldError must receive the error in an array. When a field’s error does not appear or the label is not announced, the cause is almost always the id/htmlFor pair or a mixed layer.
Stop using slow, ad-bloated tool sites! 🤮
🔎 Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)
⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free
🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!