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.