Hey everyone, I’m trying to use react-datepicker with react-hook-form and zod.
I have properly registered the zodResolver which is working for other input fields, but I am not able to figure out how to properly register and get the respective schema error for the DatePicker.
I have tried placing the ´{...register(field.name, { valueAsDate: true })}´ inside DatePicker, but that will throw an error.
I am now making use of Controller from react-hook-form, which will allow validation to be made, but the error message shown is always “Required” (even without using register) instead of being the one I set inside my schema and that was working before I implemented react-date-picker.
Below you can find the relevant snipper for how I’m trying to implement it currently, does anyone know how I can properly set or customize an error message for this?
<Controller
{...register(field.name, { valueAsDate: true })}
control={form.control}
name={field.name}
rules={{ required: 'Date and Time are required' }}
render={({ field: { onChange, value } }) => (
<DatePicker
placeholderText="Enter date and time"
selected={value as Date}
onChange={onChange}
showTimeSelect
timeFormat="HH:mm"
timeIntervals={15}
timeCaption="time"
dateFormat="MMMM d, yyyy h:mm aa"
className={`text-center ${
errors[field.name] ? 'border-red-500' : ''
}`}
/>
)}
/>