Form
A native form boundary that submits values and shows field errors.
Usage guidelines
- Wrap every group of controls that saves together in a real form.
Formrenders a native<form>; use it when the boundary needsonFormSubmitvalues, servererrors,validationModeoractionsRef. A native<form>stays correct when another form library owns submission and validation. Do not nest form owners. - Give every submit button
type="submit"and keep it enabled while the form is incomplete: submitting runs validation and marks the fields. Showloadingon the button while the request is pending. - Put a
nameon eachField. The names are the keys of the values object passed toonFormSubmitand of theerrorsobject. - Report a rejected submission at the field it concerns: map a documented error code to that field's message in
errors, rather than showing the raw server message or a toast. A rejection that belongs to no field stays inside the form as anAlert. - Keep drafts, schemas and reset behavior in the application owner. Prefer uncontrolled controls with
defaultValueunless application code must own the value while editing. - Name each form with
aria-labeloraria-labelledbywhen a page has more than one.
Single fields are covered on Field and grouped controls on Fieldset.
Anatomy
Examples
Submitting values
onFormSubmit receives the values as an object keyed by field name, and calls preventDefault on the native submit event. With the default validationMode="onSubmit", submitting validates every field first:
- Native constraints run, then each
validatefunction. - When a field is invalid, its
FieldErrorshows, the control getsaria-invalid, focus moves to the first invalid control, andonFormSubmitis not called. - After the first submission, every field validates on each change: an error clears as soon as the value is fixed, and emptying a required field shows its error right away.
Submit the empty form to see the flow, then fill it in to see the submitted values.
Server errors
Pass errors to map a rejected request to fields by name. The error shows at once in a FieldError without match, the control becomes invalid and receives focus, and the error clears when the user edits that field. Submitting again without an edit keeps the error and does not call onFormSubmit. Save with the default key to see the rejection, then enter a key that starts with sk-.
Validation timing
validationMode on Form sets when fields validate; a validationMode on a Field takes precedence. With onBlur, a field validates when focus leaves it; an empty required field is flagged only after the user has edited it, and typing hides the error until the next blur. onChange validates on every change. In every mode, submitting validates all fields, including untouched required ones. Switch between the modes below to compare when errors appear and clear.
Keyboard
| Key | Action |
|---|---|
| Enter | In a text input, submits the form. Invalid fields show their errors and focus moves to the first of them. |
API reference
Form
A native form element with consolidated client and server error handling. Renders a <form> element.
| Prop | Type | Default | Details |
|---|---|---|---|
className | string | ((state: FormState) => string | undefined) | No default | |
render | ComponentRenderFn<React.DetailedHTMLProps<React.FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>, FormState> | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | No default | |
style | React.CSSProperties | ((state: FormState) => React.CSSProperties | undefined) | No default | |
validationMode | 'onBlur' | 'onChange' | 'onSubmit' | 'onSubmit' | |
errors | Errors | No default | |
onFormSubmit | ((formValues: FormValues, eventDetails: BaseForm.SubmitEventDetails) => void) | No default | |
actionsRef | React.RefObject<FormActions | null> | No default | |