Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

App.vue

120 lines3.1 KB

1<script setup lang="ts">
2import { useForm } from '@tanstack/vue-form'
3import { type } from 'arktype'
4import { Schema as S } from 'effect'
5import * as v from 'valibot'
6import { z } from 'zod'
7import FieldInfo from './FieldInfo.vue'
8​
9const ZodSchema = z.object({
10 firstName: z
11 .string()
12 .min(3, '[Zod] You must have a length of at least 3')
13 .startsWith('A', "[Zod] First name must start with 'A'"),
14 lastName: z.string().min(3, '[Zod] You must have a length of at least 3'),
15})
16​
17const ValibotSchema = v.object({
18 firstName: v.pipe(
19 v.string(),
20 v.minLength(3, '[Valibot] You must have a length of at least 3'),
21 v.startsWith('A', "[Valibot] First name must start with 'A'"),
22 ),
23 lastName: v.pipe(
24 v.string(),
25 v.minLength(3, '[Valibot] You must have a length of at least 3'),
26 ),
27})
28​
29const ArkTypeSchema = type({
30 firstName: 'string >= 3',
31 lastName: 'string >= 3',
32})
33​
34const EffectSchema = S.standardSchemaV1(
35 S.Struct({
36 firstName: S.String.pipe(
37 S.minLength(3),
38 S.annotations({
39 message: () => '[Effect/Schema] You must have a length of at least 3',
40 }),
41 ),
42 lastName: S.String.pipe(
43 S.minLength(3),
44 S.annotations({
45 message: () => '[Effect/Schema] You must have a length of at least 3',
46 }),
47 ),
48 }),
50​
51const form = useForm({
52 defaultValues: {
53 firstName: '',
54 lastName: '',
55 },
56 validators: [
57 {
58 // Switch this run value to any of the schemas above.
59 run: ZodSchema,
60 // run: ValibotSchema,
61 // run: ArkTypeSchema,
62 // run: EffectSchema,
63 triggers: ['change'],
64 },
65 ],
66 onSubmit: async ({ value }) => {
67 console.log(value)
68 },
69})
70</script>
71​
72<template>
73 <div>
74 <h1>Standard Schema Form Example</h1>
75 <form @submit.prevent.stop="form.handleSubmit()">
76 <div>
77 <form.Field name="firstName" v-slot="{ field }">
78 <label :for="field.name">First Name:</label>
79 <input
80 :id="field.name"
81 :name="field.name"
82 :value="field.value"
83 :aria-invalid="field.meta.isInvalid"
84 @blur="field.handleBlur"
85 @input="
86 field.handleChange(($event.target as HTMLInputElement).value)
87 "
88 />
89 <FieldInfo :field="field" />
90 </form.Field>
91 </div>
92​
93 <div>
94 <form.Field name="lastName" v-slot="{ field }">
95 <label :for="field.name">Last Name:</label>
96 <input
97 :id="field.name"
98 :name="field.name"
99 :value="field.value"
100 :aria-invalid="field.meta.isInvalid"
101 @blur="field.handleBlur"
102 @input="
103 field.handleChange(($event.target as HTMLInputElement).value)
104 "
105 />
106 <FieldInfo :field="field" />
107 </form.Field>
108 </div>
109​
110 <form.Subscribe
111 :selector="(state) => [state.canSubmit, state.isSubmitting] as const"
112 v-slot="[canSubmit, isSubmitting]"
113 >
114 <button type="submit" :disabled="!canSubmit">
115 {{ isSubmitting ? '...' : 'Submit' }}
116 </button>
117 </form.Subscribe>
118 </form>
119 </div>
120</template>