Form
Accessible layout primitives for labels, controls, helper text, validation messages, sections and actions.
Installation
Install Form in your project.
npx binhlaig-ui@latest add formFor project setup, installer selection, dependencies, and advanced installation instructions, see the Installation Guide.
View Installation Guide →Update or Overwrite →Import
Import the Form layout primitives from your UI directory.
import {
Form,
FormActions,
FormControl,
FormDescription,
FormField,
FormLabel,
FormMessage,
FormSection,
FormSectionDescription,
FormSectionHeader,
FormSectionTitle,
} from "@/components/ui/form"React Hook Form + Zod
A step-by-step guide using your existing Binhlaig Form components.
1. Install dependencies
Install Form through Binhlaig UI, then add the three form packages. Input and Button must also exist in your UI folder.
npx binhlaig-ui@latest add form
npm install react-hook-form zod @hookform/resolvers2. Import the tools
React Hook Form manages values, touched fields, errors and submission state. Zod defines rules. zodResolver connects the schema to useForm.
import { useForm, Controller } from "react-hook-form"
import { z } from "zod"
import { zodResolver } from "@hookform/resolvers/zod"3. Define the schema and TypeScript type
Define rules once and infer the value type from the schema. trim() removes surrounding spaces before validation. Add a message to each rule for your users.
const schema = z.object({
name: z.string().trim().min(2, "Enter at least 2 characters."),
email: z.string().trim().email("Enter a valid email."),
})
type Values = z.infer<typeof schema>4. Connect useForm to Zod
Inside your component, pass zodResolver(schema) and defaultValues to useForm. onTouched validates after a field is blurred and then as it changes. Submit validates the complete schema.
const form = useForm<Values>({
resolver: zodResolver(schema),
defaultValues: { name: "", email: "" },
mode: "onTouched",
})5. Register native inputs and display errors
Use register() for native input, textarea, select and checkbox controls. Binhlaig FormField is a div wrapper. Link the label and error IDs explicitly; FormMessage displays the children you pass.
<FormField>
<FormLabel htmlFor="name">Name</FormLabel>
<FormControl>
<Input
id="name"
{...form.register("name")}
aria-invalid={Boolean(form.formState.errors.name)}
aria-describedby={form.formState.errors.name ? "name-error" : undefined}
/>
</FormControl>
{form.formState.errors.name && (
<FormMessage id="name-error">
{form.formState.errors.name.message}
</FormMessage>
)}
</FormField>6. Submit, reset and handle server errors
Form is already an HTML form: use handleSubmit directly. An async onSubmit keeps isSubmitting active while awaiting a request. reset() restores defaults; setError() can show an API error. Server-side validation is still required. The snippets below illustrate wiring; use your actual API and handle its response.
<Form onSubmit={form.handleSubmit(onSubmit)} noValidate>
{/* Registered fields */}
{form.formState.errors.root?.server && (
<FormMessage>{form.formState.errors.root.server.message}</FormMessage>
)}
<Button type="button" onClick={() => form.reset()}>Reset</Button>
<Button type="submit" disabled={form.formState.isSubmitting}>
{form.formState.isSubmitting ? "Saving..." : "Submit"}
</Button>
</Form>
// After a failed API response, inside onSubmit:
form.setError("root.server", { type: "server", message: "Unable to save. Try again." })7. Use Controller for controlled components
Import Controller from react-hook-form and wrap FormField inside its render function. Pass field to the actual control and fieldState.error to FormMessage. Do not pass control, name or render to Binhlaig FormField. For a custom Select, map value/onValueChange to field.value/field.onChange; for a custom Checkbox, map checked/onCheckedChange and keep a boolean value. Do not register a controlled field a second time.
Common mistakes
- Keep one Form element. Do not wrap a second form inside it or use Form as FormProvider.
- Give inputs IDs and labels matching htmlFor. Set aria-invalid and aria-describedby for errors.
- Set defaultValues to avoid undefined controlled values. Use compatible resolver and Zod package versions.
- noValidate in these demos makes schema errors visible. Add appropriate native constraints in your production form.
Usage
Build a basic form using field, label, control and action primitives.
Validation
Use FormDescription and FormMessage to communicate field state.
Form Sections
Group related fields in a bordered section.
Checkbox and Select
Form primitives work with native controls and custom UI components.
React Hook Form
Connect Binhlaig Form layout primitives to React Hook Form for typed values, validation errors and submission state.
Submit State
Disable the submit action and show progress while saving.
API Reference
Components included in the Binhlaig UI Form layout.
| Component | Purpose |
|---|---|
Form | Root form layout. |
FormField | Groups a single field. |
FormLabel | Accessible field label. |
FormControl | Wraps an input control. |
FormDescription | Displays helper text. |
FormMessage | Displays validation feedback. |
FormSection | Groups related fields. |
FormSectionHeader | Groups section heading text. |
FormSectionTitle | Section heading. |
FormSectionDescription | Section supporting text. |
FormActions | Aligns submit and cancel actions. |