Encountering an issue with using The Range Date Picker within the Form component. Specifically, I am looking to store {from, to} values of the range in an object, however, utilizing an object as a Form field value results in error messages not functioning properly; instead, I receive undefined as an error message.
To resolve this, I have made modifications to the Form Message Component to display errors in a slightly altered format, although I believe this is more of a workaround rather than the correct method.
The code snippet below illustrates the current setup:
const formSchema = z.object({
title: z.string().min(2, {
message: "Title must be at least 2 characters.",
}),
date: z.object({
from: z.date(),
to: z.date(),
}),
});
function NewEventPage() {
const form = useForm({
resolver: zodResolver(formSchema),
defaultValues: {
title: "",
date: {},
},
});
function onError(error) {
console.log(error);
/*
title:
message: "Title must be at least 2 characters."
ref: {focus: ƒ, select: ƒ, setCustomValidity: ƒ, reportValidity: ƒ}
type: "too_small"
date:
from: {message: 'Required', type: 'invalid_type', ref: undefined}
to: {message: 'Required', type: 'invalid_type', ref: undefined}
*/
}
return (
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit, onError)}
className="mx-auto mt-12 grid max-w-screen-lg grid-cols-2 gap-x-12 gap-y-8"
>
<h1 className="col-span-2 scroll-m-20 text-center text-4xl font-extrabold tracking-tight lg:text-5xl">
Create A New event
</h1>
<FormField
control={form.control}
name="title"
render={({ field }) => {
return (
<FormItem>
<FormLabel>Event Title</FormLabel>
<FormControl>
<Input placeholder="The best title ever" {...field} />
</FormControl>
<FormDescription>Give a name for your event</FormDescription>
<FormMessage />
</FormItem>
);
}}
/>
<FormField
control={form.control}
name="date"
render={({ field }) => (
<FormItem>
<FormLabel>Date</FormLabel>
<FormControl>
[...]
</FormControl>
<FormDescription>
Select the date for when the event will take place
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit" className="col-span-2">
Submit
</Button>
</form>
</Form>
);
}
An image showing that the error message appears correctly for the title but shows as undefined for the date can be viewed here: https://i.stack.imgur.com/uo365.png