react hook form onSubmit does not invoke
Answered
American Chinchilla posted this in #help-forum
American ChinchillaOP
"use client"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { z } from "zod"
import { Button } from "@/components/ui/button"
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import { Textarea } from "./ui/textarea"
import ChannelSelect from "./form/ChannelSelect"
import { addData } from "@/lib/actions"
import { Guild } from "@prisma/client"
const FormSchema = z.object({
welcomeChannel: z.string(),
welcomeMessage: z.string().min(2),
})
export default function WelcomeForm({ guild }: { guild: Guild }) {
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
})
async function onSubmit(data: z.infer<typeof FormSchema>) {
console.log(data)
await addData(guild.id, data)
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
<FormField
control={form.control}
name="welcomeChannel"
render={({ field }) => (
<FormItem>
<FormLabel>Welcome channel</FormLabel>
<ChannelSelect {...field} value={guild.welcomeChannel} />
<FormDescription>Select a channel</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="welcomeMessage"
render={({ field }) => (
<FormItem>
<FormLabel>Welcome message</FormLabel>
<FormControl>
<Textarea
placeholder="Message"
className="resize-none"
{...field}
/>
</FormControl>
<FormDescription>
The message that will be sent to the welcome channel
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
</form>
<Button type="submit">Save</Button>
</Form>
)
}"use client"
import { useCurrentGuild } from "@/app/contexts/CurrentGuildContext"
import { FormControl } from "@/components/ui/form"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
export default function ChannelSelect({
onChange,
value,
}: {
onChange: (value: string) => void
value: string | null
}) {
const { channels } = useCurrentGuild()
const textChannels = channels.filter((channel) => channel.type === 0)
return (
<Select onValueChange={onChange} defaultValue={value ? value : undefined}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select a channel" />
</SelectTrigger>
</FormControl>
<SelectContent>
{textChannels.map(({ id, name }) => (
<SelectItem key={id} value={id}>
#{name}
</SelectItem>
))}
</SelectContent>
</Select>
)
}so i have
console.log(data) inside the onSubmit function but it does not log anything to consoleAnswered by Southern rough shrimp
Your <button> is outside the scope of your <form> tag, move it up one line
4 Replies
Southern rough shrimp
Your <button> is outside the scope of your <form> tag, move it up one line
Answer
@Southern rough shrimp Your <button> is outside the scope of your <form> tag, move it up one line
American ChinchillaOP
thanks
Southern rough shrimp
Please mark the solution
American ChinchillaOP
Yea sorry