Skip to content
Gelatin UIAlpha

Use TanStack Form

Build a TanStack Form form with our Field parts. Schema checks, server errors, and a submit button that waits.

Forms · step 2 of 3 · 6 min read

Prompt for your agent

Read https://gelatinui.com/docs/forms/tanstack-form.md. Build the form I describe with TanStack Form, a schema, and Gelatin UI Field parts. Done: each field shows its error under it, server errors show under their field, and the submit button waits while the request runs.

Your agent reads /docs/forms/tanstack-form.md: the same text as this page, as Markdown.

You get four full files to copy: one field, a full form with server checks, two other control types, and a list of rows. Each file uses TanStack Form for the values and our Field parts for the look.

Before you start:

  1. Add the items: npx shadcn@latest add @gelatin-ui/form @gelatin-ui/field @gelatin-ui/button @gelatin-ui/input. Add the other controls that a file imports (select, switch, input-group) the same way.
  2. Add the packages: npm install @tanstack/react-form zod. The files use Zod 4.
  3. Write the server calls that the files import, or replace them with yours: subscribe in @/lib/newsletter, and invite, isMember, inviteAll, and updateMember in @/lib/team. Each takes the form values and returns a promise.

Anatomy

TanStack Form owns the value and the errors. Field shows them. invalid turns the field red. FieldError with match shows the message.

File: newsletter-form.tsx.

TSX

Build a form

Give your schema to useForm as one validator. With revalidateLogic, the form checks on submit first. After that, it checks on each change, so an error goes away when the value is correct.

File: invite-form.tsx.

TSX

Any Standard Schema works in validators: Zod, Valibot, ArkType, or Effect Schema.

Control types

Every control calls its change callback with the value, not with an event. Pass that value to field.handleChange(value) in an arrow function.

ControlsValueOn change
Input, Textarea, Select, Combobox, Radio Group, Checkbox Group, Number Field, Date Picker, Input OTPvalueonValueChange
Checkbox, SwitchcheckedonCheckedChange

File: member-form.tsx. It shows a Select and a Switch.

TSX

Show the server's errors

Do the server check in onSubmitAsync. Return the errors by field name. Each error shows under its field, and onSubmit does not run.

See onSubmitAsync in invite-form.tsx, in Build a form.

Wait for the submit

form.Subscribe reads isSubmitting. Pass it to the Button busy prop. The Button then shows a spinner, keeps its width, and ignores a second press.

See form.Subscribe in invite-form.tsx, in Build a form.

Arrays

Set mode="array" on a list field. pushValue adds a row, and removeValue removes a row. Each row is a separate field. Its name uses the index, and its key uses the row id.

File: emails-form.tsx.

TSX

Traps

Our own sign-in and settings forms had these bugs first. Each trap has its fix.

  • Two checks fight. Form runs the browser checks before onSubmit. With type="email", a bad email stops the submit, so TanStack Form never runs. Use type="text" with inputMode="email", and let the schema check the value.
  • FieldError needs match. With no match, FieldError shows only the browser checks. Give it the same test as invalid on Field.
  • Errors are objects. A schema gives issue objects, not strings. Show errors[0]?.message, not errors[0].
  • One submit at a time. Two fast Enter presses call handleSubmit two times. Return early while form.state.isSubmitting is true.

Next steps