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
Preview
Code
Approach
TanStack Form provides headless form state management. We combine it with:
Fieldcomponents 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:
| Mode | Description |
|---|---|
onSubmit | Validate on form submission |
onChange | Validate on every change |
onBlur | Validate 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
| Method | Description |
|---|---|
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 |