Wraps a control with a label, a hint and an error. The error is hidden until the browser marks the control :user-invalid (the user touched it and left a bad value), so there is no state and no JavaScript. Parts are linked with ids and aria-describedby. Server component.
Browser Support
Baseline Widely available2 featuresDetails
Shows the error and the red border only after the user touched the field. Without it the error never appears; native validation still blocks the submit
Lets the group react to its control: the red label and the visible error
Preview
We send the receipt here.
Enter a valid email, for example name@example.com
Installation
Usage
import { Field, FieldDescription, FieldError, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
export default function Example() {
return (
<Field>
<FieldLabel htmlFor="email">Email</FieldLabel>
<Input id="email" type="email" required aria-describedby="email-hint" />
<FieldDescription id="email-hint">We send the receipt here.</FieldDescription>
<FieldError>Enter a valid email</FieldError>
</Field>
)
}Examples
Horizontal
orientation="horizontal" puts the control and the label in a row: for checkboxes and switches.
<Field orientation="horizontal">
<Checkbox id="terms" required />
<FieldLabel htmlFor="terms">I accept the terms</FieldLabel>
</Field>Error from the server
invalid forces the error. Add aria-invalid on the control and its id to aria-describedby.
This username is taken
<Field invalid>
<FieldLabel htmlFor="username">Username</FieldLabel>
<Input id="username" aria-invalid="true" aria-describedby="username-error" />
<FieldError id="username-error">This username is taken</FieldError>
</Field>Group with a legend
FieldSet and FieldLegend make a <fieldset> for related fields; FieldGroup spaces the fields.
<FieldSet>
<FieldLegend>Account</FieldLegend>
<FieldGroup>
<Field>…</Field>
<Field>…</Field>
</FieldGroup>
</FieldSet>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| orientation | "vertical" | "horizontal" | "vertical" | Control and label stacked or in a row |
| invalid | boolean | false | Forces the invalid state and shows FieldError |
| className | string | - | Additional CSS classes |
Parts
| Part | Element | Description |
|---|---|---|
| Field | div | Wrapper, role="group", holds the invalid state |
| FieldLabel | label | Label from the registry; red when invalid, adds * for required controls |
| FieldDescription | p | Hint under the control; reference it in aria-describedby |
| FieldError | p | Error text, role="alert"; visible only when invalid |
| FieldGroup | div | Column of fields with a gap |
| FieldSet / FieldLegend | fieldset / legend | <fieldset> and <legend> for related fields |