ui
ui copied to clipboard
validateDOMNesting Warning for Form
I am using React Hook form inside radix in my component like this.
const profileFormSchema = z.object({
name: z
.string({ required_error: "Name is required" })
.min(2, {
message: "Username must be at least 2 characters.",
})
.max(30, {
message: "Username must not be longer than 30 characters.",
}),
description: z.string().optional(),
});
type ProfileFormValues = z.infer<typeof profileFormSchema>;
export function ProfileForm() {
const form = useForm<ProfileFormValues>({
resolver: zodResolver(profileFormSchema),
defaultValues: {
name: "",
description: "",
},
mode: "onChange",
});
function onSubmit(data: ProfileFormValues) {
console.log(data);
}
return (
<section>
<div className="flex flex-col justify-between gap-2 sm:flex-row">
<AlertDialog>
<AlertDialogTrigger asChild>
<Button variant="outline">Show Dialog</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
<AlertDialogDescription>
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="space-y-8"
>
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input placeholder="shadcn" {...field} />
</FormControl>
<FormDescription>
This is your public display name. It can be your
real name or a pseudonym. You can only change this
once every 30 days.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="description"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input placeholder="shadcn" {...field} />
</FormControl>
<FormDescription>
This is your public display name. It can be your
real name or a pseudonym. You can only change this
once every 30 days.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit">Update profile</Button>
</form>
</Form>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
</section>
);
}
I am getting warning like this in console
Warning: validateDOMNesting(...): <form> cannot appear as a descendant of <p>.
The AlertDialogDescription component is a paragraph element (<p>) and you should not place a form inside of it (or any other block element).
You can place the form inside the <AlertDialogContent> tag directly (since it's a div element).
p.s.: or maybe use the Dialog component instead.