Next.js Discord

Discord Forum

Working with multiple checkboxes in Nextjs14, zod, shadcn/ui and react-hook-form.

Answered
Sun bear posted this in #help-forum
Open in Discord
Original message was deleted.
Answered by Sun bear
saw this example on shadcn's website:

reservationMethod: z
        .array(z.string())
        .refine((value) => value.some((agent) => agent), {
            message: "You have to select at least one item.",
        }),
View full answer

5 Replies

Asian paper wasp
You need to be A LOT more specific on the requirements...

Here are some following questions:
1. Is that mean the checkbox(s) is a mandatory field? i.e. at least one checkbox must be checked?
2. Are the checkboxes, if multiple, belong to the same "question" in the form? e.g. Which color do you like? (1) red, (2) green, (3) blue?
Sun bear
yes at least one checkbox must be checked and they all belong to the same question
Asian paper wasp
Then the zod schema should be something like:

checkboxes: z.string().array().min(1)


Replace "checkboxes" with some thing more meaningful in your case.

string() can be replaced with something else depending on the values of the checkbox
Sun bear
saw this example on shadcn's website:

reservationMethod: z
        .array(z.string())
        .refine((value) => value.some((agent) => agent), {
            message: "You have to select at least one item.",
        }),
Answer
Sun bear
it seems to be wokring now