Binhlaig logoBinhlaig UI
Binhlaig UI Component

Form

Accessible layout primitives for labels, controls, helper text, validation messages, sections and actions.

Installation

Install Form in your project.

Terminal
npx binhlaig-ui@latest add form

For 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.

TSX / Terminal
npx binhlaig-ui@latest add form
npm install react-hook-form zod @hookform/resolvers

2. Import the tools

React Hook Form manages values, touched fields, errors and submission state. Zod defines rules. zodResolver connects the schema to useForm.

TSX / Terminal
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.

TSX / Terminal
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.

TSX / Terminal
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.

TSX / Terminal
<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.

TSX / Terminal
<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.

Issue report

Use 5 to 80 characters.

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.

We only use this email to reply.

Form Sections

Group related fields in a bordered section.

Profile information

Update your display name and bio.

Checkbox and Select

Form primitives work with native controls and custom UI components.

Receive updates about your account.

React Hook Form

Connect Binhlaig Form layout primitives to React Hook Form for typed values, validation errors and submission state.

We only use this email to reply.

Submit State

Disable the submit action and show progress while saving.

API Reference

Components included in the Binhlaig UI Form layout.

ComponentPurpose
FormRoot form layout.
FormFieldGroups a single field.
FormLabelAccessible field label.
FormControlWraps an input control.
FormDescriptionDisplays helper text.
FormMessageDisplays validation feedback.
FormSectionGroups related fields.
FormSectionHeaderGroups section heading text.
FormSectionTitleSection heading.
FormSectionDescriptionSection supporting text.
FormActionsAligns submit and cancel actions.