#How can I wrap shadcn's Dialog in react-hook-form

7 messages · Page 1 of 1 (latest)

mint bramble
#

Hey guys, I'm trying to create a modal with a form in it (specifically using shadcn's Dialog wrapped with shadcn's Form and <form>, however my <Button type="submit" /> is having no effect when I click it, any ideas?

const formSchema = z.object({
  name: z.string().min(2, {
    message: "Foo name must be at least 2 characters.",
  }),
  description: z.string().min(2, {
    message: "",
  }),
});

export function NewFooModal() {
  const form = useForm<z.infer<typeof formSchema>>({
    resolver: zodResolver(formSchema),
    defaultValues: {
      name: "",
    },
  });

  function onSubmit(values: z.infer<typeof formSchema>) {
    console.log(values)
  }

  return (...)
}```
rose tangleBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

mint bramble
#
return (
  <Form {...form}>
    <form onSubmit={form.handleSubmit(onSubmit)}>
      <Dialog>
        <DialogTrigger asChild>
          <Button>
            Click me
          </Button>
        </DialogTrigger>
        <DialogContent>
          <VisuallyHidden.Root>
            <DialogTitle>New Foo</DialogTitle>
          </VisuallyHidden.Root>
          <DialogHeader>
            <div >
              <div>
                <div>
                  <ComboboxForm />
                </div>
                <ChevronRight size={14} />
                <div>New Foo</div>
              </div>
              <div></div>
            </div>
          </DialogHeader>
          <div>
            <FormField
              control={form.control}
              name="name"
              render={({ field }) => (
                <FormItem>
                  <FormControl>
                    <Input
                      {...field}
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />
            <FormField
              control={form.control}
              name="description"
              render={({ field }) => (
                <FormItem>
                  <FormControl>
                    <Textarea
                      placeholder="Add description..."
                      {...field}
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />
            <div>
              <FormButtons />
            </div>
          </div>
          <DialogFooter>
            <Button type="submit">
              Create Foo
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    </form>
  </Form>
)
heady idol
mint bramble
#

brilliant, thanks

mint bramble