Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Form examples · Advanced

Form with Checkbox for React

A form with three checkboxes and their descriptions, where the terms box must be checked before the form submits.

Framework
UI library

Checkbox validation for required agreements: the terms box must be checked before the form submits. Each row is a horizontal Field with the Checkbox first and a FieldContent holding its FieldLabel and FieldDescription. A Controller wires each box into React Hook Form, and the Zod schema refines terms to true while marketing and security stay optional. Use it for terms acceptance, newsletter opt-ins or notification preferences.

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 terms checkbox required?

terms is z.boolean().refine((value) => value, …) with the message "You must accept the terms and conditions." While the box is unchecked, submit fails, the row's Field gets invalid and its FieldError shows the message under the description.

Do the other two boxes have to be checked?

No. marketing and security are plain z.boolean(), so either value submits. Their FieldError stays empty.

Which boxes start checked?

defaultValues sets marketing to false, security to true and terms to false. Each Checkbox is controlled by its field's value: checked={field.value} and onCheckedChange={field.onChange}.

How is each row laid out?

The three rows come from one options array mapped to a Controller each. A row is Field orientation="horizontal" with the Checkbox, then FieldContent with the FieldLabel, the FieldDescription and the FieldError. The label's htmlFor matches the checkbox's id, such as form-with-checkbox-terms.

What do the three descriptions say?

Marketing emails: "Receive emails about new products and features." Security alerts: "Receive emails about account security." Terms: "You agree to our Terms of Service and Privacy Policy."

What does Submit do?

Submit is a type="submit" button. form.handleSubmit(onSubmit) runs the Zod schema and, once terms is checked, logs { marketing, security, terms } to the console. Replace onSubmit with your own request.