How to create custom validation in a field that need value from another field using formik and yup

I use formik and yup to handle form validation in my app.

I have 2 field that related to each other, let’s say field ‘date’ and field ‘time’.

I want to make a custom validation in field ‘time’ to check whether the time of the day has passed or not based on value from field ‘date’

For example, today is 26 Feb 2021 and 08.00 AM, so that users cannot choose a time below 8 o’clock.

date: string().required('date required'),
time: string()
  .required('time is require')
  .matches(myCustomRegex)

139 thoughts on “How to create custom validation in a field that need value from another field using formik and yup”

Leave a Comment