Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

307
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!