I solve it by using .when
method.
date: string().required('date required'),
time: string()
.required('time is require')
.matches(myCustomRegex)
.when('date', {
.is: data => date && date moment(new Date(),'x').format('DD/MM/YYYY') === moment(new Date(date), 'x').format('DD/MM/YYYY')
.then: String().test(
'time',
'Start Time must not be less than the current time',
value => {
if(value){
const currentHour = new Date().getHours();
const currentMinute = new Date().getMinutes();
const userPickHour = parseInt(value.split(':')[0], 10)
const userPickMinute = parseInt(value.split(':')[1], 10);
if(userPickHour < currentHour){
return false;
}else if(userPickHour === currentHour && userPickMinute <= currentMinute){
return false;
}else {
return true;
}
}
return true;
}
)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…