Form

A native form element with consolidated error handling.

Base UI React 1.8.0
FineSoft Stencil
Submit

The Stencil comparison uses fs-form together with the migrated fs-field-* and fs-button elements. fs-form is a transparent custom-element host: it renders the native <form> that owns submission, native attributes, FormData, validation, and focus. Consumer classes, styles, global attributes, ARIA attributes, and data-* attributes are forwarded to that native form.

Pass form-level errors directly to fs-form; descendant fields resolve their messages by name and clear only the field being edited. validationMode is inherited by fields, and actionsRef.current.validate() validates all fields or one named field. A string action uses native browser submission. A function action receives FormData, while Stencil’s formSubmit property receives the consolidated JavaScript value object and the original submit event details. It is named without the React on prefix because React treats every on* prop on a custom element as a DOM event listener instead of assigning the function property. As in Base UI, either function API prevents the native submission after validation succeeds. formRef exposes the rendered native form.

The code panels below are the files used by the running Stencil examples. They contain all consumer state and behavior; there is no component-specific docs controller or hidden structure.

Anatomy

Form is composed together with Field. Import the components and place them together:

Anatomy

Examples

Submit with a Server Function

Forms using useActionState can be submitted with a Server Function instead of onSubmit.

Base UI React 1.8.0
FineSoft Stencil
Submit

Submit form values as a JavaScript object

You can use onFormSubmit instead of the native onSubmit to access form values as a JavaScript object. This is useful when you need to transform the values before submission, or integrate with 3rd party APIs.

Submission using onFormSubmit

When used, preventDefault is called on the native submit event.

Using with Zod

When parsing the schema using schema.safeParse(), the z.flattenError(result.error).fieldErrors data can be used to map the errors to each field’s name.

Base UI React 1.8.0
FineSoft Stencil
Submit

API reference

errorsErrors
Name
Description

Validation errors returned externally, typically after submission by a server or a form action. This should be an object where keys correspond to the name attribute on <Field.Root>, and values correspond to error(s) related to that field.

Type
actionsRefReact.RefObject<Form.Actions | null>
Description

A ref to imperative actions.

  • validate: Validates all fields when called. Optionally pass a field name to validate a single field.
Type
Example
onFormSubmitfunction
Description

Event handler called when the form is submitted. preventDefault() is called on the native submit event when used.

Type
validationModeForm.ValidationMode'onSubmit'
Description

Determines when the form should be validated. The validationMode prop on <Field.Root> takes precedence over this.

  • onSubmit (default): validates the field when the form is submitted, afterwards fields will re-validate on change.
  • onBlur: validates a field when it loses focus.
  • onChange: validates the field on every change to its value.
Type
Default
'onSubmit'
classNamestring | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
renderReactElement | function
Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type

Form.PropsHide

Re-Export of Form props as FormProps

Form.StateHide

Form.ActionsHide

Form.SubmitEventDetailsHide

Form.SubmitEventReasonHide

Form.ValidationModeHide

Form.ValuesHide