Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Form examples · Validation

Required Field Validation Form for React

A name and an email field marked with asterisks that must both be filled in before the form submits.

Framework
UI library

Empty submissions waste everyone's time. Name and Email are marked with an asterisk in their labels, and the Zod schema rejects an empty name with "Name is required" and anything that isn't an address with "Invalid email address". Each is a Field wired to React Hook Form by a Controller, with a FieldError for the message. Use it for contact forms, sign-ups, or any form with fields that can't be skipped.

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

How is the asterisk shown?

It's part of the label text: the FieldLabels read Name and Email . Nothing else marks the fields required.

Which message shows for an empty submit?

Both fields start as "". An empty name fails .min(1, "Name is required"), and an empty email fails z.email("Invalid email address"), so each FieldError shows its message.

Why is there no separate required message for email?

z.email treats an empty string as an invalid address, so blank and malformed emails share "Invalid email address". Add .min(1, …) before it, as the email validation example does, if blank should read differently.

What does the description under each field say?

Both FieldDescriptions say "This field is required." That line is static help text, not the validation message, which shows in the FieldError below it.

How are the two fields registered?

Each is a Controller with control={form.control} that spreads field onto its Input; Email also sets type="email". The resolver is zodResolver(formSchema), and the form's noValidate keeps the browser's own checks out of the way.

When do those checks run?

mode is the default, so they run when Submit calls form.handleSubmit(onSubmit), which logs the values only after both checks pass.