javascript - 为什么所需的检查在日期选择器中不起作用?

标签 javascript reactjs react-hooks react-hook-form

当我单击“提交”按钮时。它没有显示“必填”错误,因为它是必填字段。这是我的代码。我已经注册了“必填”,并且还尝试使用规则,但仍然没有获得所需的验证

<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/

相关文章:

javascript - 将毫秒转换为具有 UTC 时区的 UTC 日期对象

javascript - 显示全屏图像,等待 2 秒,关闭图像,打开 div - javascript/jquery

react-native - 如何使用 useState Hook 来处理来自 React Native 中 FlatList 的多个动态文本输入?

reactjs - React createContext/useContext 无法在页面之间生存

javascript - polymer 形式在 Firefox 中不支持 Ajax-ing

javascript - 使用外部js文件引用td标签

javascript - Router 6 - 从父组件调用子组件函数

javascript - React 异步调用抛出 Cannot convert undefined or null to object at Function.getOwnPropertyNames

reactjs - 在 React 中,将获取的 Firebase 数据存储在本地或全局状态中?

reactjs - 如何全局指定身份验证,而不是在 Next js 中的每个页面上定义,我使用 next-auth 包和 react 查询