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

errors
Errors
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
Errors | undefined
actionsRef
React.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
React.RefObject<Form.Actions | null> | undefined
Example
onFormSubmit
function
Description

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

Type
validationMode
Form.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'
className
string | function
Description

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

Type
style
React.CSSProperties | function
Name
Type
render
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

Additional Types

Form.Props

Re-Export of Form props as FormProps

Form.State
Form.Actions
Form.SubmitEventDetails
Form.SubmitEventReason
Form.ValidationMode
Form.Values