create-huenei-template 0.2.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config/templates.js +3 -3
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-component/SKILL.md +288 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-component/references/component-patterns.md +358 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/SKILL.md +387 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md +519 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-directives/SKILL.md +437 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-directives/references/directive-patterns.md +570 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-forms/SKILL.md +434 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-forms/references/form-patterns.md +398 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/SKILL.md +366 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md +448 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-routing/SKILL.md +395 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-routing/references/routing-patterns.md +472 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-signals/SKILL.md +302 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-signals/references/signal-patterns.md +404 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/SKILL.md +435 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md +497 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-testing/SKILL.md +660 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-testing/references/testing-patterns.md +707 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-tooling/SKILL.md +352 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-tooling/references/tooling-patterns.md +448 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-accessibility/SKILL.md +780 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-components/SKILL.md +718 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-design-systems/SKILL.md +612 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-layouts/SKILL.md +594 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/SKILL.md +432 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/references/animation-patterns.md +427 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/SKILL.md +354 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/references/troubleshooting-guide.md +306 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/SKILL.md +578 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/references/react-patterns.md +353 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/SKILL.md +608 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/advanced-theme-patterns.md +215 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/custom-utilities-guide.md +281 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-mobile-first/SKILL.md +768 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/SKILL.md +374 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/references/bundle-optimization.md +244 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-plugins/SKILL.md +505 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/SKILL.md +601 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/references/advanced-responsive-patterns.md +485 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/web-design-guidelines/SKILL.md +39 -0
- package/dist/templates/frontend/angular-base/package.json +0 -1
- package/dist/templates/frontend/angular-base/pnpm-lock.yaml +8183 -0
- package/package.json +1 -1
- package/templates/frontend/angular-base/.cursor/skills/angular-component/SKILL.md +288 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-component/references/component-patterns.md +358 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-di/SKILL.md +387 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md +519 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-directives/SKILL.md +437 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-directives/references/directive-patterns.md +570 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-forms/SKILL.md +434 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-forms/references/form-patterns.md +398 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-http/SKILL.md +366 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md +448 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-routing/SKILL.md +395 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-routing/references/routing-patterns.md +472 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-signals/SKILL.md +302 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-signals/references/signal-patterns.md +404 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-ssr/SKILL.md +435 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md +497 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-testing/SKILL.md +660 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-testing/references/testing-patterns.md +707 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-tooling/SKILL.md +352 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-tooling/references/tooling-patterns.md +448 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-accessibility/SKILL.md +780 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-components/SKILL.md +718 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-design-systems/SKILL.md +612 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-layouts/SKILL.md +594 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/SKILL.md +432 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/references/animation-patterns.md +427 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/SKILL.md +354 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/references/troubleshooting-guide.md +306 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/SKILL.md +578 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/references/react-patterns.md +353 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/SKILL.md +608 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/advanced-theme-patterns.md +215 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/custom-utilities-guide.md +281 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-mobile-first/SKILL.md +768 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/SKILL.md +374 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/references/bundle-optimization.md +244 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-plugins/SKILL.md +505 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/SKILL.md +601 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/references/advanced-responsive-patterns.md +485 -0
- package/templates/frontend/angular-base/.cursor/skills/web-design-guidelines/SKILL.md +39 -0
- package/templates/frontend/angular-base/package.json +0 -1
- package/templates/frontend/angular-base/pnpm-lock.yaml +8183 -0
- package/templates/frontend/angular-base/package-lock.json +0 -13321
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
# Angular Form Patterns
|
|
2
|
+
|
|
3
|
+
## Table of Contents
|
|
4
|
+
- [Reactive Forms (Production-Stable)](#reactive-forms-production-stable)
|
|
5
|
+
- [Typed Reactive Forms](#typed-reactive-forms)
|
|
6
|
+
- [FormBuilder Patterns](#formbuilder-patterns)
|
|
7
|
+
- [Dynamic Forms with FormArray](#dynamic-forms-with-formarray)
|
|
8
|
+
- [Custom Validators](#custom-validators)
|
|
9
|
+
- [Form State Management](#form-state-management)
|
|
10
|
+
|
|
11
|
+
## Reactive Forms (Production-Stable)
|
|
12
|
+
|
|
13
|
+
For production applications requiring stability guarantees, use Reactive Forms:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { Component, inject } from '@angular/core';
|
|
17
|
+
import { ReactiveFormsModule, FormBuilder, Validators } from '@angular/forms';
|
|
18
|
+
|
|
19
|
+
@Component({
|
|
20
|
+
selector: 'app-login',
|
|
21
|
+
imports: [ReactiveFormsModule],
|
|
22
|
+
template: `
|
|
23
|
+
<form [formGroup]="form" (ngSubmit)="onSubmit()">
|
|
24
|
+
<input formControlName="email" />
|
|
25
|
+
@if (form.controls.email.errors?.['required'] && form.controls.email.touched) {
|
|
26
|
+
<span class="error">Email is required</span>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
<input type="password" formControlName="password" />
|
|
30
|
+
|
|
31
|
+
<button type="submit" [disabled]="form.invalid">Login</button>
|
|
32
|
+
</form>
|
|
33
|
+
`,
|
|
34
|
+
})
|
|
35
|
+
export class Login {
|
|
36
|
+
private fb = inject(FormBuilder);
|
|
37
|
+
|
|
38
|
+
form = this.fb.group({
|
|
39
|
+
email: ['', [Validators.required, Validators.email]],
|
|
40
|
+
password: ['', [Validators.required, Validators.minLength(8)]],
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
onSubmit() {
|
|
44
|
+
if (this.form.valid) {
|
|
45
|
+
console.log(this.form.value);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Typed Reactive Forms
|
|
52
|
+
|
|
53
|
+
### Typed FormControl
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { FormControl } from '@angular/forms';
|
|
57
|
+
|
|
58
|
+
// Inferred type: FormControl<string | null>
|
|
59
|
+
const name = new FormControl('');
|
|
60
|
+
|
|
61
|
+
// Non-nullable (no reset to null)
|
|
62
|
+
const email = new FormControl('', { nonNullable: true });
|
|
63
|
+
// Type: FormControl<string>
|
|
64
|
+
|
|
65
|
+
// With validators
|
|
66
|
+
const username = new FormControl('', {
|
|
67
|
+
nonNullable: true,
|
|
68
|
+
validators: [Validators.required, Validators.minLength(3)],
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Typed FormGroup
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { FormGroup, FormControl } from '@angular/forms';
|
|
76
|
+
|
|
77
|
+
interface UserForm {
|
|
78
|
+
name: FormControl<string>;
|
|
79
|
+
email: FormControl<string>;
|
|
80
|
+
age: FormControl<number | null>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const form = new FormGroup<UserForm>({
|
|
84
|
+
name: new FormControl('', { nonNullable: true }),
|
|
85
|
+
email: new FormControl('', { nonNullable: true }),
|
|
86
|
+
age: new FormControl<number | null>(null),
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// Typed value access
|
|
90
|
+
const name: string = form.controls.name.value;
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### NonNullableFormBuilder
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
import { inject } from '@angular/core';
|
|
97
|
+
import { NonNullableFormBuilder } from '@angular/forms';
|
|
98
|
+
|
|
99
|
+
@Component({...})
|
|
100
|
+
export class Profile {
|
|
101
|
+
private fb = inject(NonNullableFormBuilder);
|
|
102
|
+
|
|
103
|
+
form = this.fb.group({
|
|
104
|
+
name: ['', Validators.required], // FormControl<string>
|
|
105
|
+
email: ['', [Validators.required, Validators.email]],
|
|
106
|
+
preferences: this.fb.group({
|
|
107
|
+
newsletter: [false], // FormControl<boolean>
|
|
108
|
+
theme: ['light' as 'light' | 'dark'], // FormControl<'light' | 'dark'>
|
|
109
|
+
}),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## FormBuilder Patterns
|
|
115
|
+
|
|
116
|
+
### Nested FormGroups
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
@Component({
|
|
120
|
+
imports: [ReactiveFormsModule],
|
|
121
|
+
template: `
|
|
122
|
+
<form [formGroup]="form" (ngSubmit)="onSubmit()">
|
|
123
|
+
<input formControlName="name" placeholder="Name" />
|
|
124
|
+
|
|
125
|
+
<div formGroupName="address">
|
|
126
|
+
<input formControlName="street" placeholder="Street" />
|
|
127
|
+
<input formControlName="city" placeholder="City" />
|
|
128
|
+
<input formControlName="zip" placeholder="ZIP" />
|
|
129
|
+
</div>
|
|
130
|
+
|
|
131
|
+
<button type="submit">Submit</button>
|
|
132
|
+
</form>
|
|
133
|
+
`,
|
|
134
|
+
})
|
|
135
|
+
export class Profile {
|
|
136
|
+
private fb = inject(NonNullableFormBuilder);
|
|
137
|
+
|
|
138
|
+
form = this.fb.group({
|
|
139
|
+
name: ['', Validators.required],
|
|
140
|
+
address: this.fb.group({
|
|
141
|
+
street: [''],
|
|
142
|
+
city: ['', Validators.required],
|
|
143
|
+
zip: ['', [Validators.required, Validators.pattern(/^\d{5}$/)]],
|
|
144
|
+
}),
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Dynamic Forms with FormArray
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
import { FormArray } from '@angular/forms';
|
|
153
|
+
|
|
154
|
+
@Component({
|
|
155
|
+
imports: [ReactiveFormsModule],
|
|
156
|
+
template: `
|
|
157
|
+
<form [formGroup]="form">
|
|
158
|
+
<div formArrayName="items">
|
|
159
|
+
@for (item of items.controls; track $index; let i = $index) {
|
|
160
|
+
<div [formGroupName]="i">
|
|
161
|
+
<input formControlName="product" placeholder="Product" />
|
|
162
|
+
<input formControlName="quantity" type="number" />
|
|
163
|
+
<button type="button" (click)="removeItem(i)">Remove</button>
|
|
164
|
+
</div>
|
|
165
|
+
}
|
|
166
|
+
</div>
|
|
167
|
+
<button type="button" (click)="addItem()">Add Item</button>
|
|
168
|
+
</form>
|
|
169
|
+
`,
|
|
170
|
+
})
|
|
171
|
+
export class Order {
|
|
172
|
+
private fb = inject(NonNullableFormBuilder);
|
|
173
|
+
|
|
174
|
+
form = this.fb.group({
|
|
175
|
+
items: this.fb.array([this.createItem()]),
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
get items() {
|
|
179
|
+
return this.form.controls.items;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
createItem() {
|
|
183
|
+
return this.fb.group({
|
|
184
|
+
product: ['', Validators.required],
|
|
185
|
+
quantity: [1, [Validators.required, Validators.min(1)]],
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
addItem() {
|
|
190
|
+
this.items.push(this.createItem());
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
removeItem(index: number) {
|
|
194
|
+
this.items.removeAt(index);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Custom Validators
|
|
200
|
+
|
|
201
|
+
### Sync Validator
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';
|
|
205
|
+
|
|
206
|
+
export function forbiddenValue(forbidden: string): ValidatorFn {
|
|
207
|
+
return (control: AbstractControl): ValidationErrors | null => {
|
|
208
|
+
return control.value === forbidden
|
|
209
|
+
? { forbiddenValue: { value: control.value } }
|
|
210
|
+
: null;
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Usage
|
|
215
|
+
name: ['', [Validators.required, forbiddenValue('admin')]],
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### Cross-Field Validator
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
export function passwordMatch(): ValidatorFn {
|
|
222
|
+
return (group: AbstractControl): ValidationErrors | null => {
|
|
223
|
+
const password = group.get('password')?.value;
|
|
224
|
+
const confirm = group.get('confirmPassword')?.value;
|
|
225
|
+
return password === confirm ? null : { passwordMismatch: true };
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Usage
|
|
230
|
+
form = this.fb.group({
|
|
231
|
+
password: ['', [Validators.required, Validators.minLength(8)]],
|
|
232
|
+
confirmPassword: ['', Validators.required],
|
|
233
|
+
}, { validators: passwordMatch() });
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### Async Validator
|
|
237
|
+
|
|
238
|
+
```typescript
|
|
239
|
+
import { AsyncValidatorFn } from '@angular/forms';
|
|
240
|
+
import { map, catchError, of } from 'rxjs';
|
|
241
|
+
|
|
242
|
+
export function uniqueEmail(userService: User): AsyncValidatorFn {
|
|
243
|
+
return (control: AbstractControl) => {
|
|
244
|
+
return userService.checkEmail(control.value).pipe(
|
|
245
|
+
map(exists => exists ? { emailTaken: true } : null),
|
|
246
|
+
catchError(() => of(null))
|
|
247
|
+
);
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Usage
|
|
252
|
+
email: ['',
|
|
253
|
+
[Validators.required, Validators.email], // sync validators
|
|
254
|
+
[uniqueEmail(this.userService)] // async validators
|
|
255
|
+
],
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
## Form State Management
|
|
259
|
+
|
|
260
|
+
### State Properties
|
|
261
|
+
|
|
262
|
+
```typescript
|
|
263
|
+
// Check states
|
|
264
|
+
form.valid // All validations pass
|
|
265
|
+
form.invalid // Has validation errors
|
|
266
|
+
form.pending // Async validation in progress
|
|
267
|
+
form.dirty // Value changed by user
|
|
268
|
+
form.pristine // Value not changed
|
|
269
|
+
form.touched // Control has been focused
|
|
270
|
+
form.untouched // Control never focused
|
|
271
|
+
|
|
272
|
+
// Update values
|
|
273
|
+
form.setValue({ name: 'John', email: 'john@example.com' }); // Must include all
|
|
274
|
+
form.patchValue({ name: 'John' }); // Partial update
|
|
275
|
+
|
|
276
|
+
// Reset
|
|
277
|
+
form.reset();
|
|
278
|
+
form.reset({ name: 'Default' });
|
|
279
|
+
|
|
280
|
+
// Disable/Enable
|
|
281
|
+
form.disable();
|
|
282
|
+
form.enable();
|
|
283
|
+
form.controls.email.disable();
|
|
284
|
+
|
|
285
|
+
// Mark states
|
|
286
|
+
form.markAllAsTouched(); // Show all errors
|
|
287
|
+
form.markAsPristine();
|
|
288
|
+
form.markAsDirty();
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### Value Changes Observable
|
|
292
|
+
|
|
293
|
+
```typescript
|
|
294
|
+
// Subscribe to value changes
|
|
295
|
+
form.valueChanges.subscribe(value => {
|
|
296
|
+
console.log('Form value:', value);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// Single control with debounce
|
|
300
|
+
form.controls.email.valueChanges.pipe(
|
|
301
|
+
debounceTime(300),
|
|
302
|
+
distinctUntilChanged()
|
|
303
|
+
).subscribe(email => {
|
|
304
|
+
this.validateEmail(email);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
// Status changes
|
|
308
|
+
form.statusChanges.subscribe(status => {
|
|
309
|
+
console.log('Form status:', status); // VALID, INVALID, PENDING
|
|
310
|
+
});
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
### Unified Events (Angular v21+)
|
|
314
|
+
|
|
315
|
+
```typescript
|
|
316
|
+
import {
|
|
317
|
+
ValueChangeEvent, StatusChangeEvent,
|
|
318
|
+
FormSubmittedEvent, FormResetEvent
|
|
319
|
+
} from '@angular/forms';
|
|
320
|
+
|
|
321
|
+
form.events.subscribe(event => {
|
|
322
|
+
if (event instanceof ValueChangeEvent) {
|
|
323
|
+
console.log('Value changed:', event.value);
|
|
324
|
+
}
|
|
325
|
+
if (event instanceof StatusChangeEvent) {
|
|
326
|
+
console.log('Status changed:', event.status);
|
|
327
|
+
}
|
|
328
|
+
if (event instanceof FormSubmittedEvent) {
|
|
329
|
+
console.log('Form submitted');
|
|
330
|
+
}
|
|
331
|
+
if (event instanceof FormResetEvent) {
|
|
332
|
+
console.log('Form reset');
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
## Error Display Pattern
|
|
338
|
+
|
|
339
|
+
```typescript
|
|
340
|
+
@Component({
|
|
341
|
+
template: `
|
|
342
|
+
<input formControlName="email" />
|
|
343
|
+
|
|
344
|
+
@if (form.controls.email.invalid && form.controls.email.touched) {
|
|
345
|
+
<div class="errors">
|
|
346
|
+
@if (form.controls.email.errors?.['required']) {
|
|
347
|
+
<span>Email is required</span>
|
|
348
|
+
}
|
|
349
|
+
@if (form.controls.email.errors?.['email']) {
|
|
350
|
+
<span>Invalid email format</span>
|
|
351
|
+
}
|
|
352
|
+
</div>
|
|
353
|
+
}
|
|
354
|
+
`,
|
|
355
|
+
})
|
|
356
|
+
export class Form {
|
|
357
|
+
// Helper for cleaner templates
|
|
358
|
+
hasError(controlName: string, errorKey: string): boolean {
|
|
359
|
+
const control = this.form.get(controlName);
|
|
360
|
+
return control?.hasError(errorKey) && control?.touched || false;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
## Form Submission Pattern
|
|
366
|
+
|
|
367
|
+
```typescript
|
|
368
|
+
@Component({
|
|
369
|
+
template: `
|
|
370
|
+
<form [formGroup]="form" (ngSubmit)="onSubmit()">
|
|
371
|
+
<!-- fields -->
|
|
372
|
+
<button type="submit" [disabled]="form.invalid || isSubmitting">
|
|
373
|
+
{{ isSubmitting ? 'Submitting...' : 'Submit' }}
|
|
374
|
+
</button>
|
|
375
|
+
</form>
|
|
376
|
+
`,
|
|
377
|
+
})
|
|
378
|
+
export class Form {
|
|
379
|
+
isSubmitting = false;
|
|
380
|
+
|
|
381
|
+
async onSubmit() {
|
|
382
|
+
if (this.form.invalid) {
|
|
383
|
+
this.form.markAllAsTouched();
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
this.isSubmitting = true;
|
|
388
|
+
try {
|
|
389
|
+
await this.api.submit(this.form.getRawValue());
|
|
390
|
+
this.form.reset();
|
|
391
|
+
} catch (error) {
|
|
392
|
+
// Handle error
|
|
393
|
+
} finally {
|
|
394
|
+
this.isSubmitting = false;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
```
|