当我单击“提交”按钮时。它没有显示“必填”错误,因为它是必填字段。这是我的代码。我已经注册了“必填”,并且还尝试使用规则,但仍然没有获得所需的验证
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<Controller
as={
<KeyboardDatePicker
autoOk
disableToolbar
variant="inline"
format="MM/dd/yyyy"
id={"appointmentDate"}
inputVariant="outlined"
inputRef={register({ required: true })}
label={"Appointment Date"}
required={true}
helperText={errors["appointmentDate"] && "Required..!!"}
error={errors["appointmentDate"] ? true : false}
KeyboardButtonProps={{
"aria-label": "change date"
}}
/>
}
rules={{ validate: value => value === null || "Required ..!!" }}
name={"appointmentDate"}
control={control}
/>
</MuiPickersUtilsProvider>
https://codesandbox.io/s/mui-autocomplete-with-react-hook-form-1p3x5
最佳答案
这是固定代码
<Controller
as={
<KeyboardDatePicker
autoOk
disableToolbar
variant="inline"
format="MM/dd/yyyy"
id={"appointmentDate"}
// inputRef={register({ required: true })} // not required remove this
inputVariant="outlined"
label={"Appointment Date"}
required={true}
helperText={errors["appointmentDate"] && "Required..!!"}
error={errors["appointmentDate"] ? true : false}
KeyboardButtonProps={{
"aria-label": "change date"
}}
/>
}
// rules={{ validate: value => value === null || "Required ..!!" }} // change this like below
rules={{ required: true }}
name="appointmentDate"
control={control}
/>
https://codesandbox.io/s/mui-autocomplete-with-react-hook-form-iymgr
关于javascript - 为什么所需的检查在日期选择器中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60179396/