react-datepicker 和 react-hook-forms:需要不工作

react-datepicker and react-hook-forms: required not working

React-datepicker 和 react-hook-form。我正在尝试使 react-datepicker 成为必需的,但它不起作用

<Controller
    name="resetDateTime"
    control={control}
    required
    render={({ field }) => (
        <Datetime
            onChange={(date) => field.onChange(date)}
            selected={field.value}
            inputProps={{
                placeholder: "MM-DD-YYYY HH:mm",
            }}
            viewMode="time"
        />
    )}
/>
{errors.resetDateTime && <span>This field is required</span>}

当我在没有选择任何日期时间的情况下提交表单时,我希望显示错误,但它却提交了表单

<Controller /> 没有 required 属性,您必须通过 rules 属性通过验证规则。查看 docs 了解更多信息。

<Controller
    name="resetDateTime"
    control={control}
    rules={{ required: true }}
    render={({ field }) => (
        <Datetime
            onChange={(date) => field.onChange(date)}
            selected={field.value}
            inputProps={{
                placeholder: "MM-DD-YYYY HH:mm",
            }}
            viewMode="time"
        />
    )}
/>
{errors.resetDateTime && <span>This field is required</span>}