Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Form examples · Multi Field

Two Field Form for React

A two-field contact form for a name and an email address, each with a description and its own validation error.

Framework
UI library

Contact forms need just enough fields to reach people without overwhelming them. This one asks for a name and an email: each is a Field with its FieldLabel, an Input, a FieldDescription and a FieldError, stacked in a FieldGroup. A Controller connects each input to React Hook Form, and one Zod schema checks that the name has at least 2 characters and the email is a valid address. Use it for newsletter sign-ups, contact requests or waitlists.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

FAQ

Which two fields does this form register?

name is z.string().min(2, …) with "Name must be at least 2 characters." email is z.email(…) with "Please enter a valid email address." Each has its own Controller, and the input takes the controller's field props.

What do the descriptions say?

Name's FieldDescription is "Your full name." Email's is "Your email address." The placeholders are John Doe and john@example.com.

How is the email input different from name?

It sets type="email", so phones show an email keyboard; name is a text input. Each FieldLabel has an htmlFor that matches its input's id, such as two-field-form-email.

What are the starting values?

defaultValues sets email and name to empty strings, so an empty submit fails both rules. The form is w-full max-w-md.

What does Submit pass to onSubmit?

Submit is a type="submit" button. form.handleSubmit(onSubmit) calls the handler with { email, name } only after the schema passes, and the demo logs that object to the console.

When do the name and email errors show?

useForm doesn't set mode, so the resolver runs on submit, then again on each change once a field has an error. The Field gets invalid and its FieldError shows the field's message under the description.