如何防止在 react-admins DateInput 中输入错误的日期?

问题描述

如何防止用户手动输入错误的日期,例如 2020 年 2 月 30 日?

enter image description here

import { DateInput } from "react-admin";
const validateDate = (value) => {
        console.debug('value of the date is ',value);
        // In case of wrong date like 30.Feb.2020 the value is empty,so no chance to validate the correctness.
    if(isCorrect(value)){
        return undefined
    }
    return 'error'
};


<DateInput validate={[validateDate]} source={'myDate'}></DateInput>

我尝试验证日期,但是如果错误日期只是空的(未定义),因此没有机会验证或区分空日期或错误日期。

如何防止用户输入错误的日期?

解决方法

您可以使用模块来验证日期。例如使用时刻

const isValidDate = (toTest) => moment(toTest,"D/M/YY",true).isValid()