How do I make a group of checkbox inputs have at least one required? #10797
-
I am registering a group of checkboxes, but if I put { required: true }, all checkboxes are required to be checked. How can I set it so at least one of a group of checkboxes have been checked? Here is one of the inputs:
The css makes it visually hidden. Elsewhere we show a checkmark. I'm not sure setting id to register.name is a good idea but names are unique for checkboxes so it should be ok (my colleague wrote that). register is a prop we pass in with something like:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I think the easiest way to do this is just to handle validation yourself on submit, e.g: (nb, not real code, just an example)
|
Beta Was this translation helpful? Give feedback.
-
I solved it by passing a validate function and checking if at least one checkbox was checked (using the second argument) and nested objects. |
Beta Was this translation helpful? Give feedback.
I solved it by passing a validate function and checking if at least one checkbox was checked (using the second argument) and nested objects.