export const contactFormSchema = z.object({
id: z.string().cuid().optional(),
created_at: z.date().optional(),
updated_at: z.date().optional(),
email: z.string().email(),
phone: z.string().min(10).max(14),
name: z.string().min(3).max(10),
relationship: z.string(),
avatar: z.instanceof(File).optional(),
student_id: z.string().cuid(),
});
'use client';
import { zodResolver } from '@hookform/resolvers/zod';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
function useCustomForm({ formSchema }: { formSchema: any }) {
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {},
});
return [form];
}
export default useCustomForm;
const [form] = useCustomForm({
formSchema: contactFormSchema,
});
function onSubmit(values: z.infer<typeof contactFormSchema>) {
console.log(values);
}
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="secondary">Add Contact</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle>Add Contact</DialogTitle>
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input placeholder="somename@gmail.com" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="phone"
render={({ field }) => (
<FormItem>
<FormLabel>Phone</FormLabel>
<FormControl>
<Input placeholder="eg: 06 28 29 59 30" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="relationship"
render={({ field }) => (
<FormItem>
<FormLabel>Relationship</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue={field.value}
>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select relationship" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="mother">Mother</SelectItem>
<SelectItem value="father">Father</SelectItem>
<SelectItem value="brother">Brother</SelectItem>
<SelectItem value="sister">Sister</SelectItem>
<SelectItem value="uncle">Uncle</SelectItem>
<SelectItem value="aunt">Aunt</SelectItem>
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="avatar"
render={({ field }) => (
<FormItem>
<FormLabel>Avatar</FormLabel>
<FormControl>
<>
<Input
type="file"
accept="image/png, image/jpeg"
name="avatar"
onChange={(e) => {
if (e.target.files) {
field.onChange(e.target.files[0]);
}
}}
/>
<ProgressBar
progress={progress}
setProgress={setProgress}
/>
</>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit">Submit</Button>
</form>
</Form>
</DialogContent>
</Dialog>
Hi fellow coders, Did anyone found the solution for this bug, Iβve tried everything from reddit and stack overflow but nothing working at the moment.
Ok i think he using from form package β¦ my bad sorry
Nope, it is Form, exported from shadcn/ui Form component.
The problem isnβt using two form components, one is from
react-hook-formlibrary which wraps htmlβs<form>tag, most prolly the problem is within zod schema validation.