I have two select inputs pickUpTime
and dropOffTime
in react-hook-form populated with an array of strings. The array intervalTime
is an ordered time slots with a 15 minutes interval
const IntervalTime = [
'09:00',
'09:15',
'09:30',
...
]
I want to add an error if dropOffTime
is before pickUpTime
and I thought of comparing the indexes of the selected time slots to do so
resolver: yupResolver(yup.object({
pickUpTime: yup.string().required(t('required')),
dropOffTime: yup.string().when(['pickUpTime', 'dropOffTime'], {
is: (pickUpTime, dropOffTime) =>
IntervalTime.indexOf(dropOffTime) < IntervalTime.indexOf(pickUpTime),
then: yup.string().required('Drop off time must be after pick up time'),
}),
}))
but I get Error: Cyclic dependency, node was:"dropOffTime"
2
Answers
I solved it like this:
I added a bit of validation to check if dropOffDate is after pickUpDate. In that case there's no need to check time as well.
Other than using
transform()
stated in the comments, as an alternative, you can use thetest
method. It might feel easier to use.Documentation