Form
A native form element with consolidated error handling.
View as MarkdownAnatomy
Form is composed together with Field. Import the components and place them together:
import { Field } from '@base-ui-components/react/field';
import { Form } from '@base-ui-components/react/form';
<Form>
<Field.Root>
<Field.Label />
<Field.Control />
<Field.Error />
</Field.Root>
</Form>Examples
Submit with a Server Function
Forms using useActionState can be submitted with a Server Function instead of onSubmit.
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.
<Form
onFormSubmit={async (formValues: { id: string; quantity: number }) => {
const payload = {
product_id: formValues.id,
order_quantity: formValues.quantity,
};
const response = await fetch('https://api.example.com', {
method: 'POST',
body: payload,
});
}}
/>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.
API reference
errorsErrors
—
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
nameattribute on<Field.Root>, and values correspond to error(s) related to that field.- Type
{} | undefined
onFormSubmitfunction
—
onFormSubmitfunction
—- Name
- Description
Event handler called when the form is submitted.
preventDefault()is called on the native submit event when used.- Type
| (( formValues: Record<string, any>, eventDetails: Form.SubmitEventDetails, ) => void) | undefined
refRefObject<HTMLFormElement>
—
refRefObject<HTMLFormElement>
—- Name
- Type
React.RefObject<HTMLFormElement> | undefined
styleReact.CSSProperties | function
—
styleReact.CSSProperties | function
—- Name
- Type
| React.CSSProperties | ((state: Form.State) => CSSProperties | undefined) | undefined
validationModeFormValidationMode
'onSubmit'
validationModeFormValidationMode
'onSubmit'
- Name
- Description
Determines when the form should be validated. The
validationModeprop 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
'onBlur' | 'onChange' | 'onSubmit' | undefined- Default
'onSubmit'
classNamestring | function
—
classNamestring | function
—- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
string | ((state: Form.State) => string | undefined)
renderReactElement | function
—
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
ReactElementor a function that returns the element to render.- Type
| ReactElement | ((props: HTMLProps, state: Form.State) => ReactElement)