Form

A form that collects, validates and submits its fields.

Where its data is stored.

Visibility

Installation

  1. Add the registryOnce per project
  2. Add form

Usage

Form is built from Field: give each Field a name, and the form submits the value of the control inside it under that name. Every control built on Base UI works this way, from an Input to a Select, Slider or CheckboxGroup, and so do Textarea and NativeSelect. A DatePicker keeps its value in state, so send it yourself. A FileUpload takes a name of its own, and its files reach the browser's FormData but not onFormSubmit.

onFormSubmit receives the values as an object once every field is valid, and prevents the page from reloading. onSubmit and action work as they do on a native <form>, with the browser's FormData.

Examples

Every control

Each control submits under its field's name. A checkbox or radio group is one field, with a FieldItem around each option.

We alert you as spending nears it.

Alert me by

On the first of each month.

Validation

Fields check the native constraints on their control, such as required, minLength and pattern. validate adds a check of your own: return a message when the value is invalid, or null when it's fine.

A FieldError with no children shows the browser's message for whichever check failed. Pass match with a validity state, such as valueMissing, to show your own message for one check, or customError for the messages validate returns.

Fields validate when the form is submitted, then again on every change. validationMode on the form or a field changes that to onBlur or onChange.

Lowercase letters, numbers and dashes.

Server errors

Pass the errors a server returns to errors, keyed by field name. Each shows in its field's FieldError until the value changes.

With an action

action takes a function from useActionState, so the form works with React's Server Functions.

Props

Form renders a <form> that stacks its children. Errors and invalid fields belong to each Field.

The tables leave out className, style and render. Parts built on Base UI take them as its styling and composition guides describe.

Form