@aliaksei-raketski/pi-angular-developer 0.1.0

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.
Files changed (48) hide show
  1. package/README.md +44 -0
  2. package/overlays/angular-developer/references/docs-helpers.md +36 -0
  3. package/overlays/angular-developer/scripts/get-best-practices.mjs +268 -0
  4. package/overlays/angular-developer/scripts/search-documentation.mjs +396 -0
  5. package/package.json +41 -0
  6. package/scripts/sync-angular-skill.mjs +307 -0
  7. package/skills/angular-developer/SKILL.md +133 -0
  8. package/skills/angular-developer/UPSTREAM.md +9 -0
  9. package/skills/angular-developer/data/best-practices.md +56 -0
  10. package/skills/angular-developer/references/angular-animations.md +160 -0
  11. package/skills/angular-developer/references/angular-aria.md +597 -0
  12. package/skills/angular-developer/references/cli.md +86 -0
  13. package/skills/angular-developer/references/component-harnesses.md +57 -0
  14. package/skills/angular-developer/references/component-styling.md +91 -0
  15. package/skills/angular-developer/references/components.md +117 -0
  16. package/skills/angular-developer/references/creating-services.md +97 -0
  17. package/skills/angular-developer/references/data-resolvers.md +69 -0
  18. package/skills/angular-developer/references/define-routes.md +67 -0
  19. package/skills/angular-developer/references/defining-providers.md +72 -0
  20. package/skills/angular-developer/references/di-fundamentals.md +118 -0
  21. package/skills/angular-developer/references/docs-helpers.md +36 -0
  22. package/skills/angular-developer/references/e2e-testing.md +66 -0
  23. package/skills/angular-developer/references/effects.md +83 -0
  24. package/skills/angular-developer/references/environment-configuration.md +132 -0
  25. package/skills/angular-developer/references/hierarchical-injectors.md +43 -0
  26. package/skills/angular-developer/references/host-elements.md +80 -0
  27. package/skills/angular-developer/references/injection-context.md +63 -0
  28. package/skills/angular-developer/references/inputs.md +101 -0
  29. package/skills/angular-developer/references/linked-signal.md +59 -0
  30. package/skills/angular-developer/references/loading-strategies.md +61 -0
  31. package/skills/angular-developer/references/migrations.md +30 -0
  32. package/skills/angular-developer/references/navigate-to-routes.md +69 -0
  33. package/skills/angular-developer/references/outputs.md +86 -0
  34. package/skills/angular-developer/references/reactive-forms.md +118 -0
  35. package/skills/angular-developer/references/rendering-strategies.md +44 -0
  36. package/skills/angular-developer/references/resource.md +74 -0
  37. package/skills/angular-developer/references/route-animations.md +56 -0
  38. package/skills/angular-developer/references/route-guards.md +52 -0
  39. package/skills/angular-developer/references/router-lifecycle.md +45 -0
  40. package/skills/angular-developer/references/router-testing.md +87 -0
  41. package/skills/angular-developer/references/show-routes-with-outlets.md +68 -0
  42. package/skills/angular-developer/references/signal-forms.md +907 -0
  43. package/skills/angular-developer/references/signals-overview.md +94 -0
  44. package/skills/angular-developer/references/tailwind-css.md +69 -0
  45. package/skills/angular-developer/references/template-driven-forms.md +114 -0
  46. package/skills/angular-developer/references/testing-fundamentals.md +63 -0
  47. package/skills/angular-developer/scripts/get-best-practices.mjs +268 -0
  48. package/skills/angular-developer/scripts/search-documentation.mjs +396 -0
@@ -0,0 +1,907 @@
1
+ # Signal Forms
2
+
3
+ Signal Forms are the recommended approach for handling forms in modern Angular applications (v21+). They provide a reactive, type-safe, and model-driven way to manage form state using Angular Signals.
4
+
5
+ **CRITICAL**: You MUST use Angular's new Signal Forms API for all form-related functionality. Do NOT use null as a value or type of any fields.
6
+
7
+ ## Imports
8
+
9
+ You can import the following from `@angular/forms/signals`:
10
+
11
+ ```ts
12
+ import {
13
+ form,
14
+ FormField,
15
+ submit,
16
+ // Rules for field state
17
+ disabled,
18
+ hidden,
19
+ readonly,
20
+ debounce,
21
+ // Schema helpers
22
+ applyWhen,
23
+ applyEach,
24
+ schema,
25
+ // Custom validation
26
+ validate,
27
+ validateHttp,
28
+ validateStandardSchema,
29
+ // Metadata
30
+ metadata,
31
+ } from '@angular/forms/signals';
32
+ ```
33
+
34
+ ## Creating a Form
35
+
36
+ Use the `form()` function with a Signal model. The structure of the form is derived directly from the model.
37
+
38
+ ```ts
39
+ import {Component, signal} from '@angular/core';
40
+ import {form, FormField} from '@angular/forms/signals';
41
+
42
+ @Component({
43
+ // ...
44
+ imports: [FormField],
45
+ })
46
+ export class Example {
47
+ // 1. Define your model with initial values (avoid undefined)
48
+ protected readonly userModel = signal({
49
+ name: '', // CRITICAL: NEVER use null or undefined as initial values
50
+ email: '',
51
+ age: 0, // Use 0 for numbers, NOT null
52
+ address: {
53
+ street: '',
54
+ city: '',
55
+ },
56
+ hobbies: [] as string[], // Use [] for arrays, NOT null
57
+ });
58
+
59
+ // WRONG - DO NOT DO THIS:
60
+ // badModel = signal({
61
+ // name: null, // ERROR: use '' instead
62
+ // age: null, // ERROR: use 0 instead
63
+ // items: null // ERROR: use [] instead
64
+ // });
65
+
66
+ // 2. Create the form
67
+ protected readonly userForm = form(this.userModel);
68
+ }
69
+ ```
70
+
71
+ ## Validation
72
+
73
+ Import validators from `@angular/forms/signals`.
74
+
75
+ ```ts
76
+ import {required, email, min, max, minLength, maxLength, pattern} from '@angular/forms/signals';
77
+ ```
78
+
79
+ Use them in the schema function passed to `form()`:
80
+
81
+ ```ts
82
+ userForm = form(this.userModel, (schemaPath) => {
83
+ // Required
84
+ required(schemaPath.name, {message: 'Name is required'});
85
+
86
+ // Conditional required.
87
+ required(schemaPath.name, {
88
+ when({valueOf}) {
89
+ return valueOf(schemaPath.age) > 10;
90
+ },
91
+ });
92
+ // when is only available for required
93
+ // Do NOT do this: pattern(p.name, /xxx/, {when /* ERROR */)
94
+
95
+ // Email
96
+ email(schemaPath.email, {message: 'Invalid email'});
97
+
98
+ // Min/Max for numbers
99
+ min(schemaPath.age, 18);
100
+ max(schemaPath.age, 100);
101
+
102
+ // MinLength/MaxLength for strings/arrays
103
+ minLength(schemaPath.password, 8);
104
+ maxLength(schemaPath.description, 500);
105
+
106
+ // Pattern (Regex)
107
+ pattern(schemaPath.zipCode, /^\d{5}$/);
108
+ });
109
+ ```
110
+
111
+ ## FieldState vs FormField: The Parental Requirement
112
+
113
+ It's important to understand the difference between **FormField** (the structure) and **FieldState** (the actual data/signals).
114
+
115
+ **RULE**: You must **CALL** a field as a function to access its state signals (valid, touched, dirty, hidden, etc.).
116
+
117
+ ```ts
118
+ // f is a FormField (structural)
119
+ const f = form(signal({cat: {name: 'pirojok-the-cat', age: 5}}));
120
+
121
+ f.cat.name; // FormField: You can't get flags from here!
122
+ f.cat.name.touched(); // ERROR: touched() does not exist on FormField
123
+
124
+ f.cat.name(); // FieldState: Calling it gives you access to signals
125
+ f.cat.name().touched(); // VALID: Accessing the signal
126
+ f.cat().name.touched(); // ERROR: f.cat() is state, it doesn't have children!
127
+ ```
128
+
129
+ Similarly in a template:
130
+
131
+ ```html
132
+ <!-- WRONG: Property 'hidden' does not exist on type 'FormField' -->
133
+ @if (bookingForm.hotelDetails.hidden()) { ... }
134
+
135
+ <!-- RIGHT: Call it first -->
136
+ @if (bookingForm.hotelDetails().hidden()) { ... }
137
+ ```
138
+
139
+ ## Disabled / Readonly / Hidden
140
+
141
+ Control field status using rules in the schema.
142
+
143
+ ```ts
144
+ import {disabled, readonly, hidden} from '@angular/forms/signals';
145
+
146
+ userForm = form(this.userModel, (schemaPath) => {
147
+ // Conditionally disabled
148
+ disabled(schemaPath.password, {when: ({valueOf}) => !valueOf(schemaPath.createAccount)});
149
+
150
+ // Conditionally hidden (does NOT remove from model, just marks as hidden)
151
+ hidden(schemaPath.shippingAddress, {when: ({valueOf}) => valueOf(schemaPath.sameAsBilling)});
152
+
153
+ // Readonly
154
+ readonly(schemaPath.username);
155
+ });
156
+ ```
157
+
158
+ ## Binding
159
+
160
+ Import `FormField` and use the `[formField]` directive.
161
+
162
+ ```ts
163
+ import {FormField} from '@angular/forms/signals';
164
+ ```
165
+
166
+ All props on state, such as `disabled`, `hidden`, `readonly` and `name` are bound automatically.
167
+ Do _NOT_ bind the `name` field.
168
+
169
+ **CRITICAL: FORBIDDEN ATTRIBUTES**
170
+ When using `[formField]`, you MUST NOT set the following attributes in the template (either static or bound):
171
+
172
+ - `min`, `max` (Use validators in the schema instead)
173
+ - `value`, `[value]`, `[attr.value]` on **text/number/date inputs** (Already handled by `[formField]`)
174
+ - `[attr.min]`, `[attr.max]`
175
+ - `[disabled]`, `[readonly]` (Already handled by `[formField]`)
176
+
177
+ **Exception**: Static `value` on `<input type="radio">` and `<input type="checkbox">` is **allowed and required** — it identifies which option the input represents, not the bound field value.
178
+
179
+ ```html
180
+ <!-- CORRECT: value on radio specifies which option this button represents -->
181
+ <input type="radio" value="economy" [formField]="bookingForm.package.tier" />
182
+
183
+ <!-- WRONG: value binding on a regular input -->
184
+ <input [value]="someVar" [formField]="form.name" />
185
+ ```
186
+
187
+ Do NOT do this: `<input min="1" [formField]>` or `<input [value]="val" [formField]>`.
188
+
189
+ ```html
190
+ <!-- Input -->
191
+ <input [formField]="userForm.name" />
192
+
193
+ <!-- Checkbox -->
194
+ <input type="checkbox" [formField]="userForm.isAdmin" />
195
+
196
+ <!-- Select -->
197
+ <select [formField]="userForm.country">
198
+ <option value="us">US</option>
199
+ </select>
200
+
201
+ <!-- userForm.name can NOT be nullable, because input does not accept null-->
202
+ <input [formField]="userForm.name" />
203
+ ```
204
+
205
+ ## Reactive Forms
206
+
207
+ **Do NOT import** `FormControl`, `FormGroup`, `FormArray`, or `FormBuilder` from `@angular/forms`. Signal Forms replace these concepts entirely.
208
+ Signal forms does NOT have a builder.
209
+
210
+ ## Accessing State
211
+
212
+ Each field in the form is a function that returns its state.
213
+
214
+ ```ts
215
+ // Access the field by calling it
216
+ const emailState = this.userForm.email();
217
+
218
+ // Value (WritableSignal)
219
+ const value = this.userForm().value();
220
+
221
+ // Validation State (Signals)
222
+ const isValid = this.userForm().valid();
223
+ const isInvalid = this.userForm().invalid();
224
+ const errors = this.userForm().errors(); // Array of errors
225
+ const isPending = this.userForm().pending(); // Async validation pending
226
+
227
+ // Interaction State (Signals)
228
+ const isTouched = this.userForm().touched();
229
+ const isDirty = this.userForm().dirty();
230
+
231
+ // Availability State (Signals)
232
+ const isDisabled = this.userForm().disabled();
233
+ const isHidden = this.userForm().hidden();
234
+ const isReadonly = this.userForm().readonly();
235
+ ```
236
+
237
+ IMPORTANT!: Make sure to call the field to get it state.
238
+
239
+ ```ts
240
+ form().invalid()
241
+ form.field().dirty()
242
+ form.field.subfield().touched()
243
+ form.a.b.c.d().value()
244
+ form.address.ssn().pending()
245
+ form().reset()
246
+
247
+ // The only exception is length:
248
+ form.children.length
249
+ form.length // NOTE: no parenthesis!
250
+ form.client.addresses.length // No "()"
251
+
252
+ @for (income of form.addresses; track $index) {/**/}
253
+ ```
254
+
255
+ ## Submitting
256
+
257
+ Use the `submit()` function. It automatically marks all fields as touched before running the action.
258
+
259
+ **CRITICAL**: The callback to `submit()` MUST be `async` and MUST return a Promise.
260
+
261
+ ```ts
262
+ import { submit } from '@angular/forms/signals';
263
+
264
+ // CORRECT - async callback
265
+ onSubmit() {
266
+ submit(this.userForm, async () => {
267
+ // This only runs if the form is valid
268
+ await this.apiService.save(this.userModel());
269
+ console.log('Saved!');
270
+ });
271
+ }
272
+
273
+ // WRONG - missing async keyword
274
+ onSubmit() {
275
+ submit(this.userForm, () => { // ERROR: must be async
276
+ console.log('Saved!');
277
+ });
278
+ }
279
+ ```
280
+
281
+ ## Handling Errors
282
+
283
+ `field().errors()` returns the errors array of ValidationError:
284
+
285
+ ```ts
286
+ interface ValidationError {
287
+ readonly kind: string;
288
+ readonly message?: string;
289
+ }
290
+ ```
291
+
292
+ Do _NOT_ return null from validators.
293
+ When there are no errors, return undefined
294
+
295
+ ### Context
296
+
297
+ Functions passed to rules like `validate()`, `disabled()`, `applyWhen` take a context object. It is **CRITICAL** to understand its structure:
298
+
299
+ ```ts
300
+ validate(
301
+ schemaPath.username,
302
+ ({
303
+ value, // Signal<T>: Writable current value of the field
304
+ fieldTree, // FieldTree<T>: Sub-fields (if it's a group/array)
305
+ state, // FieldState<T>: Access flags like state.valid(), state.dirty()
306
+ valueOf, // (path) => T: Read values of OTHER fields (tracking dependencies), e.g. valueOf(schemaPath.password)
307
+ stateOf, // (path) => FieldState: Access state (valid/dirty) of OTHER fields, e.g. stateOf(schemaPath.password).valid()
308
+ pathKeys, // Signal<string[]>: Path from root to this field
309
+ }) => {
310
+ // WRONG: if (touched()) ... (touched is not in context)
311
+ // RIGHT: if (state.touched()) ...
312
+
313
+ if (value() === 'admin') {
314
+ return {kind: 'reserved', message: 'Username admin is reserved'};
315
+ }
316
+ },
317
+ );
318
+ ```
319
+
320
+ ### IMPORTANT: Paths are NOT Signals
321
+
322
+ Inside the `form()` callback, `schemaPath` and its children (e.g., `schemaPath.user.name`) are **NOT** signals and are **NOT** callable.
323
+
324
+ ```ts
325
+ // WRONG - This will throw an error:
326
+ applyWhen(p.ssn, () => p.ssn().touched(), (ssnField) => { ... });
327
+
328
+ // RIGHT - Use stateOf() to get the state of a path:
329
+ applyWhen(p.ssn, ({ stateOf }) => stateOf(p.ssn).touched(), (ssnField) => { ... });
330
+
331
+ // RIGHT - Use valueOf() to get the value of a path:
332
+ applyWhen(p.ssn, ({ valueOf }) => valueOf(p.ssn) !== '', (ssnField) => { ... });
333
+ ```
334
+
335
+ ### Multiple Items
336
+
337
+ - Use `applyEach` for applying rules per item.
338
+ - **CRITICAL**: `applyEach` callback takes ONLY ONE argument (the item path), NOT two:
339
+
340
+ ```ts
341
+ // CORRECT - single argument
342
+ applyEach(s.items, (item) => {
343
+ required(item.name);
344
+ });
345
+
346
+ // WRONG - do NOT pass index
347
+ applyEach(s.items, (item, index) => {
348
+ // ERROR: callback takes 1 argument
349
+ required(item.name);
350
+ });
351
+ ```
352
+
353
+ - In the template use `@for` to iterate over the items.
354
+ - To remove an item from an array, just remove appropriate item from the array in the data.
355
+ - **`select` binding**: You CAN bind to `<select [formField]="form.country">`. Ensure options have `value` attributes.
356
+
357
+ ### Nested @for Loops
358
+
359
+ **CRITICAL**: Angular does NOT have `$parent`. In nested loops, store outer index in a variable:
360
+
361
+ ```html
362
+ <!-- WRONG - $parent does not exist -->
363
+ @for (item of form.items; track $index) { @for (option of item.options; track $index) {
364
+ <button (click)="removeOption($parent.$index, $index)">Remove</button>
365
+ <!-- ERROR -->
366
+ } }
367
+
368
+ <!-- CORRECT - use let to store outer index -->
369
+ @for (item of form.items; track $index; let outerIndex = $index) { @for (option of item.options;
370
+ track $index) {
371
+ <button (click)="removeOption(outerIndex, $index)">Remove</button>
372
+ } }
373
+ ```
374
+
375
+ ### Disabling Form Button
376
+
377
+ ```html
378
+ <button [disabled]="form().invalid() || form().pending()" />
379
+ <!-- Or -->
380
+ <button [disabled]="taxForm.invalid()" />
381
+ ```
382
+
383
+ Do NOT use `[disabled]` on an input. `[formField]` will do this.
384
+ Do NOT use `[readonly]` on an input. `[formField]` will do this.
385
+ If you need to disable or readonly a field, use `disabled()` or `readonly()` rules in the schema.
386
+
387
+ ### Async Validation
388
+
389
+ Do not use `validate()` for async, instead use `validateAsync()`:
390
+
391
+ **CRITICAL**:
392
+
393
+ 1. The `params` option MUST be a function that returns the value to validate.
394
+ 2. The `onError` handler is **REQUIRED** - it is NOT optional!
395
+
396
+ ```ts
397
+ import {resource} from '@angular/core';
398
+ import {validateAsync} from '@angular/forms/signals';
399
+
400
+ userForm = form(this.userModel, (s) => {
401
+ validateAsync(s.username, {
402
+ // 1. MUST be a function - params takes context and returns the value
403
+ params: ({value}) => value(),
404
+
405
+ // 2. Create the resource - factory receives a Signal
406
+ factory: (username) =>
407
+ resource({
408
+ params: username, // Use 'params' in resource()
409
+ loader: async ({params: value}) => {
410
+ await new Promise((resolve) => setTimeout(resolve, 1000));
411
+ return value === 'taken';
412
+ },
413
+ }),
414
+
415
+ // 3. Map success to errors
416
+ onSuccess: (isTaken) =>
417
+ isTaken ? {kind: 'taken', message: 'Username is already taken'} : undefined,
418
+
419
+ // 4. Handle errors - THIS IS REQUIRED!
420
+ onError: () => ({kind: 'error', message: 'Validation failed'}),
421
+ });
422
+ });
423
+ ```
424
+
425
+ **WRONG Examples:**
426
+
427
+ ```ts
428
+ // WRONG - params must be a function
429
+ validateAsync(s.username, {
430
+ params: s.username, // ERROR: must be ({ value }) => value()
431
+ // ...
432
+ });
433
+
434
+ // WRONG - missing onError (it's required!)
435
+ validateAsync(s.username, {
436
+ params: ({value}) => value(),
437
+ factory: (username) =>
438
+ resource({
439
+ /* ... */
440
+ }),
441
+ onSuccess: (result) => (result ? {kind: 'error'} : undefined),
442
+ // ERROR: 'onError' is missing but required!
443
+ });
444
+ ```
445
+
446
+ ### Using Resource
447
+
448
+ **CRITICAL**: In Angular's `resource()`, use `params` for the input signal.
449
+
450
+ ```ts
451
+ // CORRECT
452
+ resource({
453
+ params: mySignal,
454
+ loader: async ({params: value}) => {
455
+ /* ... */
456
+ },
457
+ });
458
+
459
+ // WRONG
460
+ resource({
461
+ request: mySignal, // ERROR: should be 'params'
462
+ loader: async ({request}) => {
463
+ /* ... */
464
+ },
465
+ });
466
+ ```
467
+
468
+ Use `debounce()` to delay synchronization between the UI and the model.
469
+
470
+ ```ts
471
+ import {debounce} from '@angular/forms/signals';
472
+
473
+ userForm = form(this.userModel, (s) => {
474
+ // Delay model updates by 300ms
475
+ debounce(s.username, 300);
476
+ });
477
+ ```
478
+
479
+ ### Conditional Validation
480
+
481
+ ```ts
482
+ form(
483
+ data,
484
+ (path) => {
485
+ applyWhen(
486
+ name,
487
+ ({value}) => value() !== 'admin',
488
+ (namePath) => {
489
+ validate(namePath.last /* ... */);
490
+ disable(namePath.last /* ... */);
491
+ },
492
+ );
493
+ },
494
+ {injector: TestBed.inject(Injector)},
495
+ );
496
+ ```
497
+
498
+ `applyWhen` passes the path mapped to the first argument.
499
+ If you need parent field, just pass it to `applyWhen`:
500
+
501
+ ```ts
502
+ form(
503
+ data,
504
+ (path) => {
505
+ applyWhen(
506
+ cat,
507
+ ({value}) => value().name !== 'admin',
508
+ (catPath) => {
509
+ require(cat.catPath /* ... */);
510
+ },
511
+ );
512
+ },
513
+ {injector: TestBed.inject(Injector)},
514
+ );
515
+ ```
516
+
517
+ ## Common Pitfalls (DO NOT DO THESE)
518
+
519
+ | Error Scenario | WRONG (Common Mistake) | RIGHT (Correct Way) |
520
+ | :--------------------- | :-------------------------------------------- | :------------------------------------------------------------------------------- |
521
+ | **Accessing Flags** | `form.field.valid()` | `form.field().valid()` |
522
+ | **Accessing value** | `form.field.value()` | `form.field().value()` |
523
+ | **Setting value** | `form.field.set(x)` | Update model signal: `this.model.update(...)` |
524
+ | **Form root flags** | `form.invalid()` | `form().invalid()` |
525
+ | **Double-calling** | `form.field()()` | `form.field().value()` |
526
+ | **Rules Context** | `({ touched }) => touched()` | `({ state }) => state.touched()` |
527
+ | **Calling Paths** | `applyWhen(p.foo, () => p.foo() === 'x')` | `applyWhen(p.foo, ({ valueOf }) => valueOf(p.foo) === 'x')` |
528
+ | **applyWhen args** | `applyWhen(condition, () => {...})` | `applyWhen(path, condition, schemaFn)` - needs 3 args |
529
+ | **Array length** | `form.items().length` | `form.items.length` (structural) |
530
+ | **Multi-select array** | `<select [formField]="form.tags">` (string[]) | Use checkboxes for array fields |
531
+ | **readonly attribute** | `<input readonly [formField]>` | Use `readonly()` rule in schema |
532
+ | **min/max attributes** | `<input min="1" max="10">` | Use `min()` and `max()` rules in schema |
533
+ | **value binding** | `<input [value]="val">` | Do NOT use `[value]` with `[formField]` (static `value` on radio/checkbox is OK) |
534
+ | **when option** | `pattern(p.x, /.../, {when: ...})` | `when` only works with `required()` |
535
+ | **Submit callback** | `submit(form, () => { ... })` | `submit(form, async () => { ... })` |
536
+ | **Async params** | `params: s.field` | `params: ({ value }) => value()` |
537
+ | **Async onError** | Omitting `onError` | `onError` is REQUIRED in `validateAsync` |
538
+ | **resource() API** | `request: signal` | `params: signal` |
539
+ | **applyEach args** | `applyEach(s.items, (item, index) => ...)` | `applyEach(s.items, (item) => ...)` |
540
+ | **Nested @for** | `$parent.$index` | Use `let outerIndex = $index` |
541
+ | **FormState import** | `import { FormState }` | `FormState` does not exist, use `FieldState` |
542
+ | **Null in model** | `signal({ name: null })` | `signal({ name: '' })` or `signal({ age: 0 })` |
543
+ | **Validate syntax** | `validate(s.field, { value } => ...)` | `validate(s.field, ({ value }) => ...)` |
544
+ | **Checkbox Array** | `[formField]="form.tags"` (string[]) | Checkboxes ONLY bind to `boolean` |
545
+
546
+ ## Big Form Example
547
+
548
+ ### `src/app/app.ts`
549
+
550
+ ```ts
551
+ import {Component, signal, ChangeDetectionStrategy} from '@angular/core';
552
+ import {
553
+ form,
554
+ FormField,
555
+ submit,
556
+ required,
557
+ email,
558
+ min,
559
+ hidden,
560
+ applyEach,
561
+ validate,
562
+ } from '@angular/forms/signals';
563
+
564
+ @Component({
565
+ selector: 'app-root',
566
+ standalone: true,
567
+ imports: [FormField],
568
+ templateUrl: './app.html',
569
+ changeDetection: ChangeDetectionStrategy.OnPush,
570
+ })
571
+ export class App {
572
+ protected readonly model = signal({
573
+ personalInfo: {
574
+ firstName: '',
575
+ lastName: '',
576
+ email: '',
577
+ age: 0,
578
+ },
579
+ tripDetails: {
580
+ destination: 'Mars',
581
+ launchDate: '',
582
+ },
583
+ package: {
584
+ tier: 'economy',
585
+ extras: [] as string[],
586
+ },
587
+ companions: [] as Array<{name: string; relation: string}>,
588
+ });
589
+
590
+ protected readonly bookingForm = form(this.model, (s) => {
591
+ required(s.personalInfo.firstName, {message: 'First name is required'});
592
+ required(s.personalInfo.lastName, {message: 'Last name is required'});
593
+ required(s.personalInfo.email, {message: 'Email is required'});
594
+ email(s.personalInfo.email, {message: 'Invalid email address'});
595
+ required(s.personalInfo.age, {message: 'Age is required'});
596
+ min(s.personalInfo.age, 18, {message: 'Must be at least 18'});
597
+
598
+ required(s.tripDetails.destination);
599
+ required(s.tripDetails.launchDate);
600
+ validate(s.tripDetails.launchDate, ({value}) => {
601
+ const date = new Date(value());
602
+ if (isNaN(date.getTime())) return undefined;
603
+ const today = new Date();
604
+ if (date < today) {
605
+ return {kind: 'pastData', message: 'Launch date must be in the future'};
606
+ }
607
+ return undefined;
608
+ });
609
+
610
+ // valueOf is used to access values of other fields in rules
611
+ hidden(s.package.extras, {when: ({valueOf}) => valueOf(s.package.tier) === 'economy'});
612
+
613
+ applyEach(s.companions, (companion) => {
614
+ required(companion.name, {message: 'Companion name required'});
615
+ required(companion.relation, {message: 'Relation required'});
616
+ });
617
+ });
618
+
619
+ addCompanion() {
620
+ this.model.update((m) => ({
621
+ ...m,
622
+ companions: [...m.companions, {name: '', relation: ''}],
623
+ }));
624
+ }
625
+
626
+ removeCompanion(index: number) {
627
+ this.model.update((m) => ({
628
+ ...m,
629
+ companions: m.companions.filter((_, i) => i !== index),
630
+ }));
631
+ }
632
+
633
+ onSubmit() {
634
+ // CRITICAL: submit callback MUST be async
635
+ submit(this.bookingForm, async () => {
636
+ console.log('Booking Confirmed:', this.model());
637
+ // If you need to do async work:
638
+ // await this.apiService.save(this.model());
639
+ });
640
+ }
641
+ }
642
+ ```
643
+
644
+ ### `src/app/app.html`
645
+
646
+ ```html
647
+ <form (submit)="onSubmit(); $event.preventDefault()">
648
+ <h1>Interstellar Booking</h1>
649
+
650
+ <section>
651
+ <h2>Personal Info</h2>
652
+
653
+ <label>
654
+ First Name
655
+ <input [formField]="bookingForm.personalInfo.firstName" />
656
+ @if (bookingForm.personalInfo.firstName().touched() &&
657
+ bookingForm.personalInfo.firstName().errors().length) {
658
+ <span>{{ bookingForm.personalInfo.firstName().errors()[0].message }}</span>
659
+ }
660
+ </label>
661
+
662
+ <label>
663
+ Last Name
664
+ <input [formField]="bookingForm.personalInfo.lastName" />
665
+ @if (bookingForm.personalInfo.lastName().touched() &&
666
+ bookingForm.personalInfo.lastName().errors().length) {
667
+ <span>{{ bookingForm.personalInfo.lastName().errors()[0].message }}</span>
668
+ }
669
+ </label>
670
+
671
+ <label>
672
+ Email
673
+ <input type="email" [formField]="bookingForm.personalInfo.email" />
674
+ @if (bookingForm.personalInfo.email().touched() &&
675
+ bookingForm.personalInfo.email().errors().length) {
676
+ <span>{{ bookingForm.personalInfo.email().errors()[0].message }}</span>
677
+ }
678
+ </label>
679
+
680
+ <label>
681
+ Age
682
+ <input type="number" [formField]="bookingForm.personalInfo.age" />
683
+ @if (bookingForm.personalInfo.age().touched() &&
684
+ bookingForm.personalInfo.age().errors().length) {
685
+ <span>{{ bookingForm.personalInfo.age().errors()[0].message }}</span>
686
+ }
687
+ </label>
688
+ </section>
689
+
690
+ <section>
691
+ <h2>Trip Details</h2>
692
+
693
+ <label>
694
+ Destination
695
+ <select [formField]="bookingForm.tripDetails.destination">
696
+ <option value="Mars">Mars</option>
697
+ <option value="Moon">Moon</option>
698
+ <option value="Titan">Titan</option>
699
+ </select>
700
+ </label>
701
+
702
+ <label>
703
+ Launch Date
704
+ <input type="date" [formField]="bookingForm.tripDetails.launchDate" />
705
+ @if (bookingForm.tripDetails.launchDate().touched() &&
706
+ bookingForm.tripDetails.launchDate().errors().length) {
707
+ <span>{{ bookingForm.tripDetails.launchDate().errors()[0].message }}</span>
708
+ }
709
+ </label>
710
+ </section>
711
+
712
+ <section>
713
+ <h2>Package</h2>
714
+
715
+ <label>
716
+ <input type="radio" value="economy" [formField]="bookingForm.package.tier" />
717
+ Economy
718
+ </label>
719
+ <label>
720
+ <input type="radio" value="business" [formField]="bookingForm.package.tier" />
721
+ Business
722
+ </label>
723
+ <label>
724
+ <input type="radio" value="first" [formField]="bookingForm.package.tier" />
725
+ First Class
726
+ </label>
727
+
728
+ @if (!bookingForm.package.extras().hidden()) {
729
+ <div>
730
+ <h3>Extras</h3>
731
+ <!-- Multi-select for arrays must use select multiple -->
732
+ <select multiple [formField]="bookingForm.package.extras">
733
+ <option value="wifi">WiFi</option>
734
+ <option value="gym">Gym</option>
735
+ </select>
736
+ </div>
737
+ }
738
+ </section>
739
+
740
+ <section>
741
+ <h2>Companions</h2>
742
+ <button type="button" (click)="addCompanion()">Add Companion</button>
743
+
744
+ @for (companion of bookingForm.companions; track $index) {
745
+ <div>
746
+ <input [formField]="companion.name" placeholder="Name" />
747
+ @if (companion.name().touched() && companion.name().errors().length) {
748
+ <span>{{ companion.name().errors()[0].message }}</span>
749
+ }
750
+
751
+ <input [formField]="companion.relation" placeholder="Relation" />
752
+ @if (companion.relation().touched() && companion.relation().errors().length) {
753
+ <span>{{ companion.relation().errors()[0].message }}</span>
754
+ }
755
+
756
+ <button type="button" (click)="removeCompanion($index)">Remove</button>
757
+ </div>
758
+ }
759
+ </section>
760
+
761
+ <button [disabled]="bookingForm().invalid()">Submit</button>
762
+ </form>
763
+ ```
764
+
765
+ ## Recovering from Build Errors
766
+
767
+ If you encounter build errors, here are the most common fixes:
768
+
769
+ ### `Property 'value' does not exist on type 'FieldTree'`
770
+
771
+ **Problem**: Accessing `.value()` directly on a field without calling it first.
772
+
773
+ ```ts
774
+ // WRONG
775
+ const val = this.form.field.value();
776
+ // RIGHT
777
+ const val = this.form.field().value();
778
+ ```
779
+
780
+ ### `Property 'set' does not exist on type 'FieldTree'`
781
+
782
+ **Problem**: Trying to set values on the form tree. Signal Forms are model-driven.
783
+
784
+ ```ts
785
+ // WRONG
786
+ this.form.address.street.set('Main St');
787
+ // RIGHT - update the model signal instead
788
+ this.model.update((m) => ({...m, address: {...m.address, street: 'Main St'}}));
789
+ ```
790
+
791
+ ### `Type 'string[]' is not assignable to type 'string'`
792
+
793
+ **Problem**: Binding `[formField]` to an array field with a single-value `<select>`.
794
+
795
+ ```html
796
+ <!-- WRONG - assignees is string[], select expects string -->
797
+ <select [formField]="form.assignees">
798
+ ...
799
+ </select>
800
+
801
+ <!-- RIGHT - Use select multiple for array fields -->
802
+ <select multiple [formField]="form.assignees">
803
+ <option value="us">US</option>
804
+ </select>
805
+ ```
806
+
807
+ ### `NG8022: Setting the 'readonly/min/max/value' attribute is not allowed`
808
+
809
+ **Problem**: Conflict between HTML attributes and `[formField]` directive.
810
+
811
+ ```html
812
+ <!-- WRONG -->
813
+ <input [formField]="form.age" min="18" max="99" />
814
+ <input [formField]="form.name" [value]="'John'" />
815
+
816
+ <!-- RIGHT - Use rules in schema -->
817
+ min(s.age, 18); max(s.age, 99); // Then just:
818
+ <input [formField]="form.age" />
819
+ ```
820
+
821
+ ### `TS2322: Type 'string[]' is not assignable to type 'boolean'`
822
+
823
+ **Problem**: Binding a checkbox to an array field instead of a boolean field.
824
+
825
+ ```html
826
+ <!-- WRONG - tags is string[] -->
827
+ <input type="checkbox" [formField]="form.tags" />
828
+
829
+ <!-- RIGHT - Use select multiple for array values -->
830
+ <select multiple [formField]="form.tags">
831
+ <option value="a">A</option>
832
+ </select>
833
+
834
+ <!-- OR - Map to boolean fields in the model -->
835
+ protected readonly model = signal({ hasWifi: false, hasGym: false });
836
+ <input type="checkbox" [formField]="form.hasWifi" />
837
+ ```
838
+
839
+ ### `'when' does not exist in type` for pattern/email/min/max
840
+
841
+ **Problem**: Using `when` option with validators other than `required`.
842
+
843
+ ```ts
844
+ // WRONG - when only works with required
845
+ pattern(s.ssn, /^\d{3}-\d{2}-\d{4}$/, {when: isJoint});
846
+
847
+ // RIGHT - use applyWhen for conditional non-required validators
848
+ applyWhen(s.ssn, isJoint, (ssnPath) => {
849
+ pattern(ssnPath, /^\d{3}-\d{2}-\d{4}$/);
850
+ });
851
+ ```
852
+
853
+ ### `Expected 3 arguments, but got 2` for applyWhen
854
+
855
+ **Problem**: Missing the path argument in `applyWhen`.
856
+
857
+ ```ts
858
+ // WRONG
859
+ applyWhen(isJoint, () => { ... });
860
+
861
+ // RIGHT - applyWhen(path, condition, schemaFn)
862
+ applyWhen(s.spouse, ({valueOf}) => valueOf(s.status) === 'joint', (spousePath) => {
863
+ required(spousePath.name);
864
+ });
865
+ ```
866
+
867
+ ### `Module has no exported member 'FormState'`
868
+
869
+ **Problem**: Importing a non-existent type.
870
+
871
+ ```ts
872
+ // WRONG
873
+ import {FormState} from '@angular/forms/signals';
874
+
875
+ // FormState does not exist. If you need type access, the form
876
+ // instance provides all necessary state through field().valid(), etc.
877
+ ```
878
+
879
+ ### `No pipe found with name 'number'` / `'json'` / `'date'`
880
+
881
+ **Problem**: Using pipes in templates.
882
+
883
+ ```html
884
+ <!-- WRONG -->
885
+ {{ totalPrice() | number:'1.2-2' }}
886
+
887
+ <!-- RIGHT - format in the component -->
888
+ protected readonly totalPriceFormatted = computed(() => this.totalPrice().toFixed(2));
889
+ <!-- then: -->
890
+ {{ totalPriceFormatted() }}
891
+ ```
892
+
893
+ ### `$parent.$index` in nested @for loops
894
+
895
+ **Problem**: Angular doesn't have `$parent`.
896
+
897
+ ```html
898
+ <!-- WRONG -->
899
+ @for (item of items; track $index) { @for (sub of item.subs; track $index) {
900
+ <button (click)="remove($parent.$index, $index)">X</button>
901
+ } }
902
+
903
+ <!-- RIGHT -->
904
+ @for (item of items; track $index; let outerIdx = $index) { @for (sub of item.subs; track $index) {
905
+ <button (click)="remove(outerIdx, $index)">X</button>
906
+ } }
907
+ ```