ui: Form in dialog doesn't send the Request.
Hello, i have a little Problem.
so thats my Code:
<Dialog open={open} onOpenChange={setOpen}>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogDescription>
Make changes to your profile here. Click save when youre done.
</DialogDescription>
</DialogHeader>
<FormField
control={form.control}
name="full_name"
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
<Input placeholder="Name" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<DialogFooter>
<Button type="submit">Submit</Button>
</DialogFooter>
</DialogContent>
</form>
</Form>
</Dialog>
That looks like
The Problem now is, that the Form doesnt send the Request. It should be, or am i wrong ?
When i move the <Form> into the <DialogContent> the Form works but the styles are wrong
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 8
- Comments: 20
IF YOU ARE USING ANY KIND OF FORM RESOLVER: Apart from this above solutions on form there might be issue in your zod schema.If you forgot to add an required zod field schema in the form then there would’nt be any error in form but internally zod stop you from submitting the form.
If you are rendering the form outside the
Dialog.Content, what is happening is that the button is being portaled to the body and as it is not inside the form element the submit will not work. What you can do is:Dialog.ContentTo resolve this I moved the
<Form {...form}/>component to wrap the entire<Dialog>component and placed the submit button inside the<DialogContent>.Here’s the code:
@daddotmn use button onClick and dispatch submit event with bubbles true
I found this: https://github.com/react-hook-form/react-hook-form/issues/566#issuecomment-612328736
There seem to be several solutions.
handleSubmit()function in the component which includes your submit button: invoke it directlyhandleSubmit(onSubmit)().<form id="my-form" >, and add this id to theformattribute of your submit button:<button type="submit" form="my-form" />.Option 1 seems easiest and solved my problem. Option 2 triggered a full page refresh/rerender so i didn’t use it.
I have a similar setup, but I’ve separated the Form into its own component and nested it into the Dialog as shown below:
It ends up looking like this:
Yes it’s working for me. What I did is that I separately made the form and then imported that into the dialog component and it’s working since then, have been using this same approach wherever needed and it’s working.
Thank you! Solved it, there is no need to wrap around Dialog box. There was a problem with zod resolvers in my case and that fixed the problem.