Form
FormReact Hook Form integration with Zod validation and accessible error messages.
Responsive viewport — resize the window to explore. Direction and density apply only to this preview. Theme applies to the docs too.
"use client";
import {
Form,
FormField,
FormItem,
FormLabel,
FormControl,
FormDescription,
FormMessage,
} from "@dashboardpack/core/components/ui/form";
import { Input } from "@dashboardpack/core/components/ui/input";
import { Button } from "@dashboardpack/core/components/ui/button";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import { useState } from "react";
const schema = z.object({
name: z
.string()
.min(2, "Use at least two characters.")
.max(40, "Use at most 40 characters."),
});
export default function FormExample() {
const form = useForm<z.infer<typeof schema>>({
resolver: zodResolver(schema),
defaultValues: { name: "" },
});
const [saved, setSaved] = useState("");
return (
<Form {...form}>
<form
onSubmit={form.handleSubmit((values) =>
setSaved(`Saved ${values.name}`),
)}
className="w-full max-w-sm space-y-5"
noValidate
>
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Workspace name</FormLabel>
<FormControl>
<Input placeholder="Apex Studio" {...field} />
</FormControl>
<FormDescription>
Use between 2 and 40 characters.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit">Save workspace</Button>
<p role="status" className="text-sm text-muted-foreground">
{saved}
</p>
</form>
</Form>
);
}Usage
Create a form with useForm and a Zod resolver, then pass the methods to Form. Compose FormField, FormItem, FormLabel, FormControl, FormDescription, and FormMessage for each field.
import {
Form,
FormField,
FormItem,
FormLabel,
FormControl,
FormDescription,
FormMessage,
} from "@dashboardpack/core/components/ui/form";Available in the included @dashboardpack/core package. Copy the complete example from Source code for all imports and state. Add "use client" when using hooks or event handlers. See installation and theming for project setup.
API reference
Common props and defaults. Slot-specific props are named below; native elements also accept their standard HTML attributes. The component TypeScript definitions are the complete type reference.
| Prop | Type / Default | Description |
|---|---|---|
| ...methods (Form) | UseFormReturnDefault: required | FormProvider receives useForm methods. |
| control (FormField) | Control<T>Default: required | The form’s control object. |
| name (FormField) | FieldPath<T>Default: required | Registered field name. |
| render (FormField) | Controller render callbackDefault: required | Return the labeled field composition. |
| children (FormMessage) | ReactNodeDefault: field error | Optional custom error message. |