Open History to see the last commit
1import { Component } from '@angular/core'
2import { TanStackField, injectForm, injectStore } from '@tanstack/angular-form'
3import * as v from 'valibot'
4import { z } from 'zod'
5import { Schema as S } from 'effect'
6β
7const ZodSchema = z.object({
8 firstName: z
9 .string()
10 .min(3, '[Zod] You must have a length of at least 3')
11 .startsWith('A', "[Zod] First name must start with 'A'"),
12 lastName: z.string().min(3, '[Zod] You must have a length of at least 3'),
13})
14β
15const ValibotSchema = v.object({
16 firstName: v.pipe(
17 v.string(),
18 v.minLength(3, '[Valibot] You must have a length of at least 3'),
19 v.startsWith('A', "[Valibot] First name must start with 'A'"),
20 ),
21 lastName: v.pipe(
22 v.string(),
23 v.minLength(3, '[Valibot] You must have a length of at least 3'),
24 ),
25})
26β
27const EffectSchema = S.standardSchemaV1(
28 S.Struct({
29 firstName: S.String.pipe(
30 S.minLength(3),
31 S.annotations({
32 message: () => '[Effect/Schema] You must have a length of at least 3',
33 }),
34 ),
35 lastName: S.String.pipe(
36 S.minLength(3),
37 S.annotations({
38 message: () => '[Effect/Schema] You must have a length of at least 3',
39 }),
40 ),
41 }),
42)
43β
44@Component({
45 selector: 'app-root',
46 standalone: true,
47 imports: [TanStackField],
48 template: `
49 <form (submit)="handleSubmit($event)">
50 <div>
51 <ng-container
52 [tanstackField]="form"
53 name="firstName"
54 #firstName="field"
55 >
56 <label [for]="firstName.api.name">First Name:</label>
57 <input
58 [id]="firstName.api.name"
59 [name]="firstName.api.name"
60 [value]="firstName.api.state.value"
61 (blur)="firstName.api.handleBlur()"
62 (input)="firstName.api.handleChange($any($event).target.value)"
63 />
64 @if (firstName.api.state.meta.isTouched) {
65 @for (error of firstName.api.state.meta.errors; track $index) {
66 <div style="color: red">
67 {{ error.message }}
68 </div>
69 }
70 }
71 @if (firstName.api.state.meta.isValidating) {
72 <p>Validating...</p>
73 }
74 </ng-container>
75 </div>
76 <div>
77 <ng-container [tanstackField]="form" name="lastName" #lastName="field">
78 <label [for]="lastName.api.name">Last Name:</label>
79 <input
80 [id]="lastName.api.name"
81 [name]="lastName.api.name"
82 [value]="lastName.api.state.value"
83 (blur)="lastName.api.handleBlur()"
84 (input)="lastName.api.handleChange($any($event).target.value)"
85 />
86 @if (firstName.api.state.meta.isTouched) {
87 @for (error of firstName.api.state.meta.errors; track $index) {
88 <div style="color: red">
89 {{ error.message }}
90 </div>
91 }
92 }
93 @if (firstName.api.state.meta.isValidating) {
94 <p>Validating...</p>
95 }
96 </ng-container>
97 </div>
98 <button type="submit" [disabled]="!canSubmit()">
99 {{ isSubmitting() ? '...' : 'Submit' }}
100 </button>
101 <button type="reset" (click)="form.reset()">Reset</button>
102 </form>
103 `,
104})
105export class AppComponent {
106 form = injectForm({
107 defaultValues: {
108 firstName: '',
109 lastName: '',
110 },
111β
112 validators: {
113 // DEMO: You can switch between schemas seamlessly
114 onChange: ZodSchema,
115 // onChange: ValibotSchema,
116 // onChange: EffectSchema,
117 },
118 onSubmit({ value }) {
119 // Do something with form data
120 console.log(value)
121 },
122 })
123β
124 canSubmit = injectStore(this.form, (state) => state.canSubmit)
125 isSubmitting = injectStore(this.form, (state) => state.isSubmitting)
126β
127 handleSubmit(event: SubmitEvent) {
128 event.preventDefault()
129 event.stopPropagation()
130 this.form.handleSubmit()
131 }
132}