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

App.svelte

127 lines3.3 KB

1<script lang="ts">
2 import { createForm } from '@tanstack/svelte-form'
3 import { type } from 'arktype'
4 import { Schema as S } from 'effect'
5 import * as v from 'valibot'
6 import { z } from 'zod'
7 import FieldInfo from './FieldInfo.svelte'
8​
9 const 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​
17 const 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​
29 const ArkTypeSchema = type({
30 firstName: 'string >= 3',
31 lastName: 'string >= 3',
32 })
33​
34 const 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​
51 const form = createForm(() => ({
52 defaultValues: {
53 firstName: '',
54 lastName: '',
55 },
56 validators: [
57 {
58 // Switch between these Standard Schema implementations seamlessly.
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​
71 void ValibotSchema
72 void ArkTypeSchema
73 void EffectSchema
74</script>
75​
76<div>
77 <h1>Standard Schema Form Example</h1>
78 <form
79 onsubmit={(event) => {
80 event.preventDefault()
81 event.stopPropagation()
82 form.handleSubmit()
83 }}
84 >
85 <form.Field name="firstName">
86 {#snippet children(field)}
87 <div>
88 <label for={field.name}>First Name:</label>
89 <input
90 id={field.name}
91 name={field.name}
92 value={field.value}
93 onblur={field.handleBlur}
94 oninput={(event) => field.handleChange(event.currentTarget.value)}
95 aria-invalid={field.meta.isInvalid}
96 />
97 <FieldInfo {field} />
98 </div>
99 {/snippet}
100 </form.Field>
101 <form.Field name="lastName">
102 {#snippet children(field)}
103 <div>
104 <label for={field.name}>Last Name:</label>
105 <input
106 id={field.name}
107 name={field.name}
108 value={field.value}
109 onblur={field.handleBlur}
110 oninput={(event) => field.handleChange(event.currentTarget.value)}
111 aria-invalid={field.meta.isInvalid}
112 />
113 <FieldInfo {field} />
114 </div>
115 {/snippet}
116 </form.Field>
117 <form.Subscribe
118 selector={(state) => [state.canSubmit, state.isSubmitting] as const}
119 >
120 {#snippet children([canSubmit, isSubmitting])}
121 <button type="submit" disabled={!canSubmit}>
122 {isSubmitting ? '...' : 'Submit'}
123 </button>
124 {/snippet}
125 </form.Subscribe>
126 </form>
127</div>