# React Hook Form with shadcn/ui

> Use React Hook Form with shadcn/ui FormField, controlled components, Zod validation, default values, and accessible error messages.

Source: https://react-hook-form.com/integrations/shadcn-ui

The [shadcn/ui Form component](https://ui.shadcn.com/docs/components/form) composes React Hook Form's [`FormProvider`](https://react-hook-form.com/docs/formprovider) and [`Controller`](https://react-hook-form.com/docs/usecontroller/controller). It supplies accessible labels, descriptions, and error messages around shadcn/ui controls.

## Installation

Add the shadcn/ui form primitives, then install the resolver and schema library used by this example:

```bash
npx shadcn@latest add form input button
npm install @hookform/resolvers zod
```

## Complete example

```tsx
"use client"

import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { z } from "zod"
import { Button } from "@/components/ui/button"
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"),
})

export function UsernameForm() {
  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>
          )}
        />
        <Button type="submit">Save</Button>
      </form>
    </Form>
  )
}
```

## Two different Form components

The shadcn/ui `Form` above provides React Hook Form context. It is different from React Hook Form's submission-oriented [`Form` component](https://react-hook-form.com/docs/useform/form), which can send values to an endpoint or Server Action.

## Native and controlled inputs

- Use `register` directly when a component exposes a native input ref and standard change events.
- Use shadcn/ui `FormField` or React Hook Form's `Controller` for controlled components such as custom selects, date pickers, and switches.
- Define every controlled field in `defaultValues`; avoid `undefined` as a controlled value.
- Spread `field` onto the actual control so `value`, `onChange`, `onBlur`, `name`, and `ref` stay connected.

See the dedicated [Zod guide](https://react-hook-form.com/integrations/zod), [`Controller`](https://react-hook-form.com/docs/usecontroller/controller), and [`FormProvider`](https://react-hook-form.com/docs/formprovider).
