Form
Accessible form field primitives built on react-hook-form with Zod validation.
Installation
npx radk-cli@latest add formAlso install the resolver:
npm install @hookform/resolvers zodUsage
import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { z } from "zod"
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"const formSchema = z.object({
username: z.string().min(2).max(50),
})
export function ProfileForm() {
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: { username: "" },
})
function onSubmit(values: z.infer<typeof formSchema>) {
console.log(values)
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="username"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input placeholder="shadcn" {...field} />
</FormControl>
<FormDescription>This is your public display name.</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit">Submit</Button>
</form>
</Form>
)
}Components
| Component | Description |
|---|---|
Form | Wraps FormProvider from react-hook-form |
FormField | Wraps Controller, provides field context |
FormItem | Layout wrapper providing unique IDs |
FormLabel | Label wired to input via htmlFor, turns red on error |
FormControl | Passes id, aria-* to the underlying input |
FormDescription | Helper text below the input |
FormMessage | Displays Zod/RHF error messages |