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
errors
Errors
Errors
- Name
- Description
An object where the keys correspond to the
name
attribute of the form fields, and the values correspond to the error(s) related to that field.- Type
Errors
onClearErrors
function
function
- Name
- Description
Event handler called when the
errors
object is cleared.- Type
((errors: Errors) => void)
className
string | 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)
render
ReactElement | 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
ReactElement
or 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 result.error.flatten().fieldErrors
data can be used to map the errors to each field’s name
.