Overview
Form provides a wrapper around form controls with built-in validation, error messaging, and accessibility features. It handles form state, validation messages, and proper ARIA attributes automatically.Features
- Built-in client-side and server-side validation
- Automatic error message display
- Accessible by default with proper ARIA attributes
- Customizable validity state handling
- Works with native HTML form elements
- Supports custom validation messages
- Integration with browser’s constraint validation API
Installation
Anatomy
API Reference
Root
Contains all the form component parts. Renders a<form> element.
Event handler called when the form is submitted.
A function to clear server errors. This is called when the user interacts with a field.
Change the default rendered element for the one passed as a child.
Field
Groups a label, control, and message together. Automatically manages IDs and ARIA attributes.The name of the field. This is used to match the field with validation messages.
When true, indicates the field has a server error.
Change the default rendered element for the one passed as a child.
Label
The label associated with a form control.Change the default rendered element for the one passed as a child.
Control
The form control (input, textarea, select, etc.). This should be used with native form elements or custom form controls.Change the default rendered element for the one passed as a child.
Message
Displays validation messages for a field.match
'valueMissing' | 'typeMismatch' | 'patternMismatch' | 'tooLong' | 'tooShort' | 'rangeUnderflow' | 'rangeOverflow' | 'stepMismatch' | 'badInput' | 'valid' | ((validity: ValidityState, formData: FormData) => boolean)
Used to target a specific validity state or provide a custom matcher function. When the matcher succeeds, the message will be shown.
When true, always shows the message regardless of validity state.
Change the default rendered element for the one passed as a child.
ValidityState
Provides access to the field’s validity state. Useful for custom UI based on validation state.A render prop that receives the validity state.
Submit
The submit button for the form.Change the default rendered element for the one passed as a child.
Example
Accessibility
Automatically manages ARIA attributes including
aria-invalid, aria-describedby, and proper label associations.Features
- Automatic ID generation for labels and controls
- Error messages are properly associated with controls
- Invalid states are announced to screen readers
- Works with browser’s built-in form validation
- Submit button is properly disabled during submission