TanStack Form
Build forms with TanStack Form and Standard Schema validation.
This guide shows how to build forms using TanStack Form with Kanpeki's Field component and schema validation.
Demo
Approach
TanStack Form provides headless form state management. We combine it with:
Fieldcomponents for layout and accessibilityTextFieldfor automatic ID wiring between label, input, description, and error- Any Standard Schema library (Zod, Valibot, ArkType) for validation — no adapters needed
- Real-time validation feedback
Anatomy
TextField is Kanpeki's React Aria behavior adapter. It automatically wires Field.Label, Field.Description, and Field.Error via aria-labelledby and aria-describedby. See the forms architecture for more.
Setup
Install dependencies
Replace zod with any Standard Schema-compliant library such as valibot or arktype — TanStack Form supports them all natively with no extra adapters.
Create a schema
Define your form shape with your schema library of choice. This example uses Zod:
Setup the form
Use useForm with your schema passed to validators:
Validation
Validation Modes
TanStack Form supports different validation strategies:
Displaying Errors
Use <Field.Error /> with the errors prop. It renders nothing when the array is empty, so no conditional render is needed:
Schema transforms (e.g. z.string().transform(Number)) are not applied to the value received in onSubmit. If you need the transformed output, call schema.parse(value) manually inside onSubmit.
Field Types
Input
Textarea
Select
Pass <Select.Root /> as the render prop on <Field.Root />. Use value and onChange for TanStack Form wiring.
Checkbox
For checkbox groups, use mode="array" and pass <Checkbox.Provider /> as the render prop on each <Field.Root />:
Switch
Submit Button State
Use <form.Subscribe /> to disable the submit button while submitting or when the form is invalid:
Always pass selector before children on <form.Subscribe />. Swapping the order causes TypeScript to lose inference on the selector's return type, resulting in type errors throughout the render function.
canSubmit is false when validation fails or a submission is already in flight. isSubmitting is true while onSubmit is pending.
Resetting the Form
Use form.reset() to reset to default values:
Array Fields
TanStack Form supports dynamic array fields with mode="array":
Array Helpers
Linked Fields
When one field's validation depends on another (e.g. confirm password), use onChangeListenTo to re-run validation when the watched field changes:
On This Page