I have a date input field in zod controller:
<I18nProvider locale="de-DE">
<Controller
control={control}
name='eventDate'
render={({ field: { value, onChange } }) => (
<DateInput
value={value}
defaultValue={event.eventDate}
onChange={onChange}
isRequired
name='eventDate'
label={"My Date"}
/>
)}
/>
</I18nProvider>
And a custom zod validation because the input return a CalendarDate object:
export const EventSchema = z.object({
eventDate: z.custom<CalendarDate>((val) => val != undefined, { message: "not valid" }),
});
Submitting the form the validation works. I save the value in a stage management storage.
If I went back to the form page, the “defaultValue” from state storage is displayed in the input field.
But if I send the form again, the validation fails and the error is displayed.
It looks the zod validation ignores the defaultValue in the field.
If I make a change in the date input field (onChange) and then submit the form again, the validation passes without failure.
I don’t know how to change the code so that after get back to form with the defaultValue from storage, the validation is ok without manually change the value so zod recognize the input value.
After get back to form with the defaultValue from storage, the validation should be ok after submit the form again without manually change the input value so zod recognize with onChange method the valid value.
Basti0186 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.