question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validate at least one checkbox (boolean) is chosen

See original GitHub issue
checkbox_fields_-_dummy_com
const schema = yup.object({
  red: yup.boolean(),
  orange: yup.boolean(),
  green: yup.boolean()
})

Implementing react-formal-material-ui, and trying to figure out how to require at least one of the options is chosen. How would you go about doing this? If it is custom/external, how to inject the validation error back into the react-formal form?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:10
  • Comments:30 (4 by maintainers)

github_iconTop GitHub Comments

183reactions
yantakuscommented, Sep 18, 2017

@DeBraid

yup.object({
  terms: yup
    .boolean()
    .oneOf([true], 'Must Accept Terms and Conditions'),
});
58reactions
sbreilercommented, Jan 15, 2019

@berdyshev Something like this worked for me:

const schema = yup
  .object({
    red: yup.boolean(),
    orange: yup.boolean(),
    green: yup.boolean()
  })
  .test(
    'myCustomTest',
    null,
    (obj) => {
      if ( obj.red || obj.orange || obj.green ) {
        return true; // everything is fine
      }

      return new yup.ValidationError(
        'Please check one checkbox',
        null,
        'myCustomFieldName'
      );
    }
  );

Check it here

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to validate that at least one checkbox should be ...
I want to do validation for checkboxes here without form tag. At least one checkbox should be selected. <div *ngFor="let item of officeLIST">...
Read more >
Angular Validate At Least One Checkbox Was Selected
Angular formArray/formGroup - Validate at least one checkbox was selected ... isLoadingCategory: boolean;. categories: ProductCategory[];. form: FormGroup;.
Read more >
At least one checkbox must be selected (checked) - Tech Notes
There is a form with multiple checkboxes and we're going to make sure that at least one is checked using pure JavaScript.
Read more >
How to validate at least one checkbox has been clicked?
For the checkbox, i used selectBooleanCheckbox. My problem here is that the user ... "At least one checkbox must be selected. "); message....
Read more >
At least one checkbox required.
Set an OnChange event in your Checkbox. There check if the checkbox is selected or not, if it is selected increment your Local...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found