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

293
Vistas
How to run validation only if element exists? Formik+Yup

I'm using Formik+Yup for creating a form and validation. My form is multipage and it has next-button, which should be disabled until all required fields are filled up correctly(with no validation error). Everything was pretty simple(I was just using validateOnMount and disabling button just with !formik.isValid) until I came to the page with radio buttons which should pop up another question(also with radio buttons) depending on your answer on previous question.

If I use validateOnMount all validations run (even questions that would not pop up) so it is impossible to activate the button. Using validateOnBlur or OnChange also did not work for same reason. I found the solution only by writing really long and hard 'if' statemen which looks like this

disabled={
!formik.values.covidStatus ||
(formik.values.covidStatus === 'covidYes' && !formik.values.igTest) ||
(formik.values.covidStatus === 'covidYes' && formik.values.igTest === 'IgYes' &&
(!formik.values.testDate || !formik.values.igNumber)) || 
(formik.values.covidStatus === 'covidYes' && formik.values.igTest === 'IgNo' &&
 !formik.values.covidDate) 
}

I tried to write custom validation for radio questions and question is: Is it possible to determine when custom validation will run? To be precise I want that validation to run only when field exists.

about 4 years ago · Juan Pablo Isaza
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