# Checkbox Group

> Multiple checkboxes collected into a string array with a min-selection rule.

A shadcn/ui block from the [@flx](https://ui.flexnative.com) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/flx/rhf-fields-05).

- Page, with a live preview: https://shadcn.exchange/r/flx/rhf-fields-05
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/flx/rhf-fields-05-light-1f476ada8d58bf4b.png
- Registry item: https://ui.flexnative.com/r/rhf-fields-05.json
- Listed: 2026-10-13, source last changed 2026-10-13

## Install

```bash
npx shadcn@latest add https://ui.flexnative.com/r/rhf-fields-05.json
```

Run it in a project that has shadcn/ui set up. It adds the files below and installs what they depend on.

## Dependencies

- npm packages: `@hookform/resolvers`, `react-hook-form`, `sonner`, `zod`
- Registry items: `field`, `checkbox`, `button`

## Files

### `registry/forms/react-hook-form/fields/rhf-fields-05.tsx`

```tsx
'use client'

import { Controller, useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import { toast } from 'sonner'
import * as z from 'zod'

import { Button } from '@/components/ui/button'
import { Checkbox } from '@/components/ui/checkbox'
import {
  Field,
  FieldContent,
  FieldError,
  FieldGroup,
  FieldLabel,
  FieldLegend,
  FieldSet,
} from '@/components/ui/field'

const ITEMS = [
  { id: 'product', label: 'Product updates' },
  { id: 'security', label: 'Security alerts' },
  { id: 'marketing', label: 'Marketing emails' },
] as const

const formSchema = z.object({
  topics: z.array(z.string()).min(1, 'Select at least one topic.'),
})

type FormValues = z.infer<typeof formSchema>

export function RhfFields05() {
  const form = useForm<FormValues>({
    resolver: zodResolver(formSchema),
    defaultValues: { topics: [] },
  })

  function onSubmit(data: FormValues) {
    toast.success('Subscriptions saved', {
      description: data.topics.join(', '),
    })
  }

  return (
    <form
      onSubmit={form.handleSubmit(onSubmit)}
      className="flex w-full max-w-sm flex-col gap-6"
    >
      <FieldGroup>
        <Controller
          control={form.control}
          name="topics"
          render={({ field, fieldState }) => (
            <FieldSet data-invalid={fieldState.invalid}>
              <FieldLegend>Email subscriptions</FieldLegend>
              <FieldGroup className="gap-2">
                {ITEMS.map((item) => (
                  <Field key={item.id} orientation="horizontal">
                    <Checkbox
                      id={`rhf-fields-05-${item.id}`}
                      checked={field.value.includes(item.id)}
                      onCheckedChange={(checked) =>
                        field.onChange(
                          checked
                            ? [...field.value, item.id]
                            : field.value.filter((v) => v !== item.id),
                        )
                      }
                    />
                    <FieldContent>
                      <FieldLabel htmlFor={`rhf-fields-05-${item.id}`}>
                        {item.label}
                      </FieldLabel>
                    </FieldContent>
                  </Field>
                ))}
              </FieldGroup>
              {fieldState.invalid && <FieldError errors={[fieldState.error]} />}
            </FieldSet>
          )}
        />
      </FieldGroup>
      <Button type="submit" size="sm">
        Save
      </Button>
    </form>
  )
}
```
