Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

300
Vistas
Yup validation requiring at least one selection

I'm trying to require one of two checkboxes to be selected in a form. The following doesn't seem to work according to the documentation:

yearGroups: yup.object().shape({
          primary: yup.bool().when('yearGroups.primary', {
            is: false,
            then: yup
              .bool()
              .oneOf([true], '')
              .required()
          }),
          secondary: yup.bool().when('yearGroups.secondary', {
            is: false,
            then: yup
              .bool()
              .oneOf([true], '')
              .required()
          }),
        }),
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Try this one. See the explanation here:

const schema = yup.object().shape({
  yearGroups: yup.object().shape(
    {
      primary: yup.bool().when("secondary", {
        is: (secondary) => !secondary,
        then: yup.bool().oneOf([true], "At least one needs to be checked")
      }),
      secondary: yup.bool().when("primary", {
        is: (primary) => !primary,
        then: yup.bool().oneOf([true], "At least one needs to be checked")
      })
    },
    [
      ["primary", "secondary"],
      ["secondary", "primary"]
    ]
  )
});

Live Demo

Codesandbox Demo

about 4 years ago · Juan Pablo Isaza Denunciar

0

This is some kind of duplication, but here are solutions: using .test() function or create your own Method - .addMethod

  1. .test function, example here
  2. .addMethod function, example here

I am using .addMethod in my code, it's more flexible, you can define all params you need. Once you defined it in the file, you can call it everywhere you need.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda