Form

Building forms with React Hook Form and Zod.

shadcn/ui docs

This is your public display name.

Installation

pnpm dlx shadcn@latest add https://neobrutalism.dev/r/form.json

Usage

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,
  FormDescription,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from '@/components/ui/form'
import { Input } from '@/components/ui/input'
const formSchema = z.object({
  username: z.string().min(2, {
    message: 'Username must be at least 2 characters.',
  }),
})
 
const form = useForm<z.infer<typeof formSchema>>({
  resolver: zodResolver(formSchema),
  defaultValues: {
    username: "",
  },
});
 
function onSubmit(values: z.infer<typeof formSchema>) {
  console.log(values);
}
<Form {...form}>
  <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
    <FormField
      control={form.control}
      name="username"
      render={({ field }) => (
        <FormItem>
          <FormLabel>Username</FormLabel>
          <FormControl>
            <Input placeholder="ekmas" {...field} />
          </FormControl>
          <FormDescription>This is your public display name.</FormDescription>
          <FormMessage />
        </FormItem>
      )}
    />
    <Button type="submit">Submit</Button>
  </form>
</Form>

Examples

Default

This is your public display name.

Basic

A simple form with a text input and a textarea. On submit, the form data is validated against a Zod schema and any errors are displayed next to each field.

Bug Report
Help us improve by reporting bugs you encounter.
0/100 characters

Include steps to reproduce, expected behavior, and what actually happened.

Input

For input fields, spread the field object onto the <Input /> component. Wrapping it in <FormControl /> wires up the id, aria-describedby and aria-invalid attributes, and <FormMessage /> shows the validation error.

Profile Settings
Update your profile information below.

This is your public display name. Must be between 3 and 10 characters. Must only contain letters, numbers, and underscores.

Textarea

For textarea fields, spread the field object onto the <Textarea /> component inside <FormControl />.

Personalization
Customize your experience by telling us more about yourself.

Tell us more about yourself. This will be used to help us personalize your experience.

Select

For select components, use field.value and field.onChange on the <Select /> component and wrap the <SelectTrigger /> in <FormControl />. Pass items to <Select /> so <SelectValue /> renders the label instead of the raw value.

Language Preferences
Select your preferred spoken language.

For best results, select the language you speak.

Checkbox

For checkbox arrays, use field.value and onCheckedChange with array manipulation. Give each <Checkbox /> its own <FormItem /> and <FormControl /> so its label and error state are wired up.

Notifications
Manage your notification preferences.

Get notified for requests that take time, like research or image generation.

Get notified when tasks you've created have updates.

Radio Group

For radio groups, use field.value and onValueChange on the <RadioGroup /> component. Each <RadioGroupItem /> gets its own <FormItem /> and <FormControl />.

Subscription Plan
See pricing and features for each plan.

You can upgrade or downgrade your plan at any time.

For everyday use with basic features.

For advanced AI usage with more features.

For large teams and heavy usage.

Switch

For switches, use field.value and onCheckedChange on the <Switch /> component.

Security Settings
Manage your account security preferences.

Enable multi-factor authentication to secure your account.

Complex Forms

Here is an example of a more complex form with multiple fields and validation.

You're almost there!
Choose your subscription plan and billing period.

Choose your subscription plan.

For individuals and small teams

For businesses with higher demands

Choose how often you want to be billed.

Select additional features you'd like to include.

Advanced analytics and reporting

Automated daily backups

24/7 premium customer support

Receive email updates about your subscription

Array Fields

React Hook Form provides a useFieldArray hook for managing dynamic array fields. This is useful when you need to add or remove fields dynamically.

Contact Emails
Manage your contact email addresses.

Add up to 5 email addresses where we can contact you.