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
{} | undefined
onFormSubmit

function

Description

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

Type
| ((
    formValues: Record<string, any>,
    eventDetails: Form.SubmitEventDetails,
  ) => void)
| undefined
ref

RefObject<HTMLFormElement>

Name
Type
React.RefObject<HTMLFormElement> | undefined
style

React.CSSProperties | function

Name
Type
| React.CSSProperties
| ((state: Form.State) => CSSProperties | undefined)
| undefined
validationMode

FormValidationMode

'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
'onBlur' | 'onChange' | 'onSubmit' | undefined
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
string | ((state: Form.State) => string | undefined)
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
| ReactElement
| ((props: HTMLProps, state: Form.State) => ReactElement)