Open History to see the last commit
1<script setup lang="ts">
2import { useForm } from '@tanstack/vue-form'
3import { type } from 'arktype'
4import * as v from 'valibot'
5import { z } from 'zod'
6import { Schema as S } from 'effect'
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 }),
49)
50β
51const form = useForm({
52 defaultValues: {
53 firstName: '',
54 lastName: '',
55 },
56 validators: {
57 // DEMO: You can switch between schemas seamlessly
58 onChange: ZodSchema,
59 // onChange: ValibotSchema,
60 // onChange: ArkTypeSchema,
61 // onChange: EffectSchema,
62 },
63 onSubmit: async ({ value }) => {
64 // Do something with form data
65 alert(JSON.stringify(value))
66 },
67})
68</script>
69β
70<template>
71 <form
72 @submit="
73 (e) => {
74 e.preventDefault()
75 e.stopPropagation()
76 form.handleSubmit()
77 }
78 "
79 >
80 <div>
81 <form.Field name="firstName">
82 <template v-slot="{ field, state }">
83 <label :htmlFor="field.name">First Name:</label>
84 <input
85 :id="field.name"
86 :name="field.name"
87 :value="field.state.value"
88 @input="
89 (e) => field.handleChange((e.target as HTMLInputElement).value)
90 "
91 @blur="field.handleBlur"
92 />
93 <FieldInfo :state="state" />
94 </template>
95 </form.Field>
96 </div>
97 <div>
98 <form.Field name="lastName">
99 <template v-slot="{ field, state }">
100 <label :htmlFor="field.name">Last Name:</label>
101 <input
102 :id="field.name"
103 :name="field.name"
104 :value="field.state.value"
105 @input="
106 (e) => field.handleChange((e.target as HTMLInputElement).value)
107 "
108 @blur="field.handleBlur"
109 />
110 <FieldInfo :state="state" />
111 </template>
112 </form.Field>
113 </div>
114 <form.Subscribe>
115 <template v-slot="{ canSubmit, isSubmitting }">
116 <button type="submit" :disabled="!canSubmit">
117 {{ isSubmitting ? '...' : 'Submit' }}
118 </button>
119 </template>
120 </form.Subscribe>
121 </form>
122</template>