Skip to contents

Form

A native form element with consolidated error handling.

View as Markdown

Anatomy

Form is composed together with Field. Import the components and place them together:

Anatomy

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.

Submission using onFormSubmit

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
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 name attribute on <Field.Root>, and values correspond to error(s) related to that field.

Type
actionsRefReact.RefObject<Form.Actions | null>
Description

A ref to imperative actions.

  • validate: Validates all fields when called. Optionally pass a field name to validate a single field.
Type
Example
onFormSubmitfunction
Description

Event handler called when the form is submitted. preventDefault() is called on the native submit event when used.

Type
validationModeForm.ValidationMode'onSubmit'
Description

Determines when the form should be validated. The validationMode prop 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
Default
'onSubmit'
classNamestring | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function
Name
Type
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 ReactElement or a function that returns the element to render.

Type

Form.PropsHide

Re-Export of Form props as FormProps

Form.StateHide

Form.ActionsHide

Form.SubmitEventDetailsHide

Form.SubmitEventReasonHide

Form.ValidationModeHide

Form.ValuesHide