Open History to see the last commit
1---
2id: quick-start
3title: Quick Start
4---
5
6TanStack Form is a headless, type-safe form library. It owns form state and
7validation while leaving markup, styling, and component choice to you.
8
9The Form v2 Vue adapter requires Vue 3.6 or newer. At the time of writing, Vue
103.6 is available as a release candidate but has not yet been released as
11stable. We hope it will be stable by the time TanStack Form v2 becomes stable.
12
13Vue 3.6 provides the component and composition behavior needed for Form
14Composition to work correctly and to preserve form and field types across
15templates and extracted components. Setting this minimum in v2 from the start
16avoids introducing a breaking Vue version requirement later.
17
18Install the Vue adapter:
19
20```bash
21npm install @tanstack/vue-form
22```
23
24## Create a form
25
26`useForm` requires `defaultValues`. Their shape becomes the form's inferred
27value type.
28
29```vue
30<script setup lang="ts">
31import { useForm } from '@tanstack/vue-form'
32
33const form = useForm({
34 defaultValues: {
35 fullName: '',
36 age: 0,
37 },
38 onSubmit: async ({ value }) => {
39 console.log(value)
40 },
41})
42</script>
43
44<template>
45 <form @submit.prevent="form.handleSubmit()">
46 <form.Field
47 name="fullName"
48 :validators="[
49 {
50 triggers: ['change', 'blur'],
51 run: ({ value }) =>
52 value.trim() ? undefined : 'Enter your full name',
53 },
54 ]"
55 v-slot="{ field }"
56 >
57 <label :for="field.name">Full name</label>
58 <input
59 :id="field.name"
60 :name="field.name"
61 :value="field.value"
62 :aria-invalid="field.meta.isInvalid"
63 @blur="field.handleBlur"
64 @input="field.handleChange(($event.target as HTMLInputElement).value)"
65 />
66 <span v-for="error in field.errors" :key="error.message" role="alert">
67 {{ error.message }}
68 </span>
69 </form.Field>
70
71 <form.Field
72 name="age"
73 :validators="[
74 {
75 triggers: ['change'],
76 run: ({ value }) =>
77 value >= 13 ? undefined : 'You must be at least 13',
78 },
79 ]"
80 v-slot="{ field }"
81 >
82 <label :for="field.name">Age</label>
83 <input
84 :id="field.name"
85 :name="field.name"
86 type="number"
87 :value="field.value"
88 :aria-invalid="field.meta.isInvalid"
89 @blur="field.handleBlur"
90 @input="
91 field.handleChange(($event.target as HTMLInputElement).valueAsNumber)
92 "
93 />
94 </form.Field>
95
96 <form.Subscribe
97 :selector="(state) => [state.canSubmit, state.isSubmitting] as const"
98 v-slot="[canSubmit, isSubmitting]"
99 >
100 <button type="submit" :disabled="!canSubmit || isSubmitting">
101 {{ isSubmitting ? 'Saving…' : 'Save' }}
102 </button>
103 </form.Subscribe>
104 </form>
105</template>
106```
107
108The important pieces are:
109
110- `defaultValues` define the complete initial value and drive type inference.
111- `form.Field` subscribes to one field. Its slot exposes `field.value`,
112 `field.meta`, and event handlers.
113- Validators are ordered objects with a `run` function and explicit
114 `triggers`.
115- `form.Subscribe` rerenders only its slot when the selected form state
116 changes.
117- `form.handleSubmit()` validates the form before calling `onSubmit`.