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>API reference
errorsErrors
Errors- Name
- Description
- An object where the keys correspond to the - nameattribute of the form fields, and the values correspond to the error(s) related to that field.
- Type
- {} | undefined
onClearErrorsfunction
function- Name
- Description
- Event handler called when the - errorsobject is cleared.
- Type
- ((errors: Errors) => void) | undefined
classNamestring | function
string | 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)
renderReactElement | function
ReactElement | 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)
Examples
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.