1. Docs
  2. Forms
  3. TanStack Form

TanStack Form

Build forms with TanStack Form and Zod validation.

This guide shows how to build forms using TanStack Form with Kanpeki's Field component and Zod schema validation.

Demo

Approach

TanStack Form provides headless form state management. We combine it with:

  • Field components for layout and accessibility
  • Zod for schema validation
  • Real-time validation feedback

Anatomy

Setup

Install dependencies

Create a schema

Define your form shape with Zod:

Setup the form

Use useForm with Zod validation:

Validation

Validation Modes

TanStack Form supports different validation strategies:

ModeDescription
onSubmitValidate on form submission
onChangeValidate on every change
onBlurValidate when field loses focus

Displaying Errors

Use Field.Error with the errors prop:

Field Types

Input

Textarea

Select

Checkbox

For checkbox arrays, use mode="array":

Switch

Resetting the Form

Use form.reset() to reset to default values:

Array Fields

TanStack Form supports dynamic array fields with mode="array":

Array Helpers

MethodDescription
field.pushValue(item)Add item to end of array
field.removeValue(i)Remove item at index
field.insertValue(i)Insert item at index
field.swapValues(a,b)Swap items at indices