Skip to content

Form

A native form boundary that submits values and shows field errors.

Optional summary visible to workspace members.

hero.tsx

Usage guidelines

  • Wrap every group of controls that saves together in a real form. Form renders a native <form>; use it when the boundary needs onFormSubmit values, server errors, validationMode or actionsRef. A native <form> stays correct when another form library owns submission and validation. Do not nest form owners.
  • Give every submit button type="submit" and keep it enabled while the form is incomplete: submitting runs validation and marks the fields. Show loading on the button while the request is pending.
  • Put a name on each Field. The names are the keys of the values object passed to onFormSubmit and of the errors object.
  • Report a rejected submission at the field it concerns: map a documented error code to that field's message in errors, rather than showing the raw server message or a toast. A rejection that belongs to no field stays inside the form as an Alert.
  • Keep drafts, schemas and reset behavior in the application owner. Prefer uncontrolled controls with defaultValue unless application code must own the value while editing.
  • Name each form with aria-label or aria-labelledby when a page has more than one.

Single fields are covered on Field and grouped controls on Fieldset.

Anatomy

tsx
tsx

Examples

Submitting values

onFormSubmit receives the values as an object keyed by field name, and calls preventDefault on the native submit event. With the default validationMode="onSubmit", submitting validates every field first:

  1. Native constraints run, then each validate function.
  2. When a field is invalid, its FieldError shows, the control gets aria-invalid, focus moves to the first invalid control, and onFormSubmit is not called.
  3. After the first submission, every field validates on each change: an error clears as soon as the value is fixed, and emptying a required field shows its error right away.

Submit the empty form to see the flow, then fill it in to see the submitted values.

Only visible to workspace admins.

Scopes
submit.tsx

Server errors

Pass errors to map a rejected request to fields by name. The error shows at once in a FieldError without match, the control becomes invalid and receives focus, and the error clears when the user edits that field. Submitting again without an edit keeps the error and does not call onFormSubmit. Save with the default key to see the rejection, then enter a key that starts with sk-.

Keys start with sk-.

server-errors.tsx

Validation timing

validationMode on Form sets when fields validate; a validationMode on a Field takes precedence. With onBlur, a field validates when focus leaves it; an empty required field is flagged only after the user has edited it, and typing hides the error until the next blur. onChange validates on every change. In every mode, submitting validates all fields, including untouched required ones. Switch between the modes below to compare when errors appear and clear.

Validation mode
validation-mode.tsx

Keyboard

KeyAction
EnterIn a text input, submits the form. Invalid fields show their errors and focus moves to the first of them.

API reference

Form

A native form element with consolidated client and server error handling. Renders a <form> element.

Base UI Form docs