-
How to use the rhf control in a Date picker field? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
Take a look at this codesandbox <Controller
control={control}
name="date"
rules={{
validate: {
min: (date) => isFuture(date) || "Please, enter a future date"
}
}}
render={({ field: { ref, onBlur, name, ...field }, fieldState }) => (
<DatePicker
{...field}
inputRef={ref}
label="Date"
renderInput={(inputProps) => (
<TextField
{...inputProps}
onBlur={onBlur}
name={name}
error={!!fieldState.error}
helperText={fieldState.error?.message}
/>
)}
/>
)}
/> |
Beta Was this translation helpful? Give feedback.
-
just addition, if a user can save with no value and for the initial value that won't trigger highlight error on its textField you can simply add conditional to defaultValue props >
|
Beta Was this translation helpful? Give feedback.
-
To integrate react-hook-form with Material-UI (v5.10) DatePicker, use the Controller component from react-hook-form. Wrap the DatePicker component inside Controller, specifying the render prop for rendering and providing name and control props. Ensure the defaultValue attribute corresponds to the initial date value. Example code snippet: To know more, read here: https://purecode.ai/blogs/mui-select/ |
Beta Was this translation helpful? Give feedback.
Take a look at this codesandbox