Open History to see the last commit
1import { ChangeDetectionStrategy, Component } from '@angular/core'
2import {
3 TanStackField,
4 injectForm,
5 injectSelector,
6} from '@tanstack/angular-form'
7import { type } from 'arktype'
8import { Schema as S } from 'effect'
9import * as v from 'valibot'
10import { z } from 'zod'
11β
12const ZodSchema = z.object({
13 firstName: z
14 .string()
15 .min(3, '[Zod] You must have a length of at least 3')
16 .startsWith('A', "[Zod] First name must start with 'A'"),
17 lastName: z.string().min(3, '[Zod] You must have a length of at least 3'),
18})
19const ValibotSchema = v.object({
20 firstName: v.pipe(v.string(), v.minLength(3), v.startsWith('A')),
21 lastName: v.pipe(v.string(), v.minLength(3)),
22})
23const ArkTypeSchema = type({
24 firstName: 'string >= 3',
25 lastName: 'string >= 3',
26})
27const EffectSchema = S.standardSchemaV1(
28 S.Struct({
29 firstName: S.String.pipe(S.minLength(3)),
30 lastName: S.String.pipe(S.minLength(3)),
31 }),
32)
33β
34@Component({
35 selector: 'app-root',
36 standalone: true,
37 changeDetection: ChangeDetectionStrategy.OnPush,
38 imports: [TanStackField],
39 template: `
40 <main>
41 <h1>Standard Schema Form Example</h1>
42 <p>Swap the schema assigned to the validator in the component source.</p>
43 <form (submit)="handleSubmit($event)">
44 <ng-container
45 [tanstackField]="form"
46 name="firstName"
47 #firstName="field"
48 >
49 <label>
50 <span>First Name</span>
51 <input
52 [name]="firstName.api.name"
53 [value]="firstName.api.value"
54 (blur)="firstName.api.handleBlur()"
55 (input)="firstName.api.handleChange($any($event).target.value)"
56 [attr.aria-invalid]="firstName.api.meta.isInvalid"
57 />
58 @for (error of firstName.api.errors; track error) {
59 <small role="alert">{{ error.message }}</small>
60 }
61 </label>
62 </ng-container>
63 <ng-container [tanstackField]="form" name="lastName" #lastName="field">
64 <label>
65 <span>Last Name</span>
66 <input
67 [name]="lastName.api.name"
68 [value]="lastName.api.value"
69 (blur)="lastName.api.handleBlur()"
70 (input)="lastName.api.handleChange($any($event).target.value)"
71 [attr.aria-invalid]="lastName.api.meta.isInvalid"
72 />
73 @for (error of lastName.api.errors; track error) {
74 <small role="alert">{{ error.message }}</small>
75 }
76 </label>
77 </ng-container>
78 <button type="submit" [disabled]="!canSubmit()">
79 {{ isSubmitting() ? '...' : 'Submit' }}
80 </button>
81 </form>
82 </main>
83 `,
84})
85export class AppComponent {
86 form = injectForm({
87 defaultValues: { firstName: '', lastName: '' },
88 validators: [{ run: ZodSchema, triggers: ['change'] }],
89 onSubmit: async ({ value }) => console.log(value),
90 })
91 canSubmit = injectSelector(this.form, (state) => state.canSubmit)
92 isSubmitting = injectSelector(this.form, (state) => state.isSubmitting)
93β
94 handleSubmit(event: SubmitEvent) {
95 event.preventDefault()
96 event.stopPropagation()
97 this.form.handleSubmit()
98 }
99β
100 // Keep the alternative Standard Schema implementations type-checked.
101 alternativeSchemas = [ValibotSchema, ArkTypeSchema, EffectSchema]
102}