Forms
How to build forms people get right the first time. Every control sits in a Field with a visible label, errors name the fix, and the spacing is built in.
This guide decides which control asks for a value, how it's labelled, and when and how errors appear. It applies to anything people type into, pick from or switch on. The parts are on Field and Input.
Ask one question: does the change wait for Save?
| Answer | Use | Why |
|---|---|---|
| Yes, it's saved with the form | Input, Textarea, Select, RadioGroup, Checkbox | People review, then commit |
| No, it applies the moment it changes | Switch, on its own or in a settings list | No Save button to wait for |
A Switch never sits in a form with a Save button, and a form field never saves on its own without saying so.
Every control sits in a Field
import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from '@/components/ui/field';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
<form className="flex flex-col gap-6">
<FieldGroup>
<Field>
<FieldLabel htmlFor="agent-name">Agent name</FieldLabel>
<Input id="agent-name" placeholder="Research assistant" />
<FieldDescription>Shown in chats and notifications.</FieldDescription>
</Field>
<Field>
<FieldLabel htmlFor="agent-email">Email for reports</FieldLabel>
<Input id="agent-email" type="email" aria-invalid="true" aria-describedby="agent-email-error" />
<FieldError id="agent-email-error">Enter a full email address, like name@example.com.</FieldError>
</Field>
</FieldGroup>
<div className="flex justify-end gap-2">
<Button variant="secondary" type="button">Cancel</Button>
<Button type="submit">Create agent</Button>
</div>
</form>Field puts the label 2px above the control and help or error text 8px below. FieldGroup spaces fields 16px apart, FieldSet sections 24px apart. Don't add your own margins.
The control follows the number of options
| People… | Use |
|---|---|
| Type one line | Input with the right type: email, url, tel, number, search |
| Type several lines | Textarea |
| Pick one of 2–5, all visible | RadioGroup |
| Pick one of about 5–15 | Select |
| Agree, or pick several | Checkbox |
Checkboxes and switches sit beside their label: <Field orientation="horizontal"> with a FieldContent for the label and help text.
Labels stay visible; placeholders are examples
- Every control has a visible label in sentence case, linked with
htmlForandid. - Help text says what people need before they type: the format, or why it's asked.
- If most fields are required, mark the optional ones ("Note (optional)"). If most are optional, mark the required ones with
<FieldLabel required>. - Don't use the placeholder as the label; it disappears as people type.
Validate when people leave a field, not while they type
- Check a field when it loses focus, and all fields on submit. Never on every keystroke.
- On submit, move focus to the first field with an error.
- Keep the error until the value is fixed, then remove it right away.
Errors name the fix
Write the message as what to do: "Enter a full email address, like name@example.com", not "Invalid input". One error per field, below it, in coral with a coral border. Never color alone: the message is always there.
Disabled, read-only and saving
- Disabled: set
disabledon the control; Field softens its label and help. Say why nearby if it isn't obvious. - Read-only:
readOnlyon Input keeps the value focusable and copyable. - Saving:
<Button type="submit" loading>while the form submits; leave the fields as they are.
One column, sized to the content
- One column by default. Two short related fields side by side:
<FieldGroup className="grid sm:grid-cols-2">. - Match the width to the content: a code field is narrow, a description full width.
mdcontrols in forms;lg(40px) for single focused tasks like onboarding and on mobile.- The primary action goes last, on the right (Actions).
Accessibility
Mark errors with aria-invalid="true" and link the message with aria-describedby; FieldError announces it to screen readers. Labels linked with htmlFor also make the text clickable, which doubles the target for checkboxes and radios.
Not covered here
Which component to pick for a value is Choosing a component. Error colors are Color usage. Confirmations and toasts after saving will be in a Feedback guide.
Checklist
- Every control is in a Field with a linked, visible label.
- Errors use
aria-invalid+FieldErrorand say how to fix the value. - No Switch inside a form with a Save button.
- No placeholder-only labels and no hand-added margins between fields.