angular-forms

Angular Forms - Quick Reference

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "angular-forms" with this command: npx skills add claude-dev-suite/claude-dev-suite/claude-dev-suite-claude-dev-suite-angular-forms

Angular Forms - Quick Reference

Deep Knowledge: Use mcp__documentation__fetch_docs with technology: angular , topic: forms for comprehensive documentation.

Typed Reactive Forms (Angular 14+)

import { Component, inject } from '@angular/core'; import { ReactiveFormsModule, FormBuilder, Validators } from '@angular/forms';

@Component({ standalone: true, imports: [ReactiveFormsModule], template: <form [formGroup]="form" (ngSubmit)="onSubmit()"> <input formControlName="name" /> <input formControlName="email" type="email" /> <button type="submit" [disabled]="form.invalid">Save</button> </form> }) export class UserFormComponent { private fb = inject(FormBuilder);

form = this.fb.nonNullable.group({ name: ['', [Validators.required, Validators.minLength(2)]], email: ['', [Validators.required, Validators.email]], age: [0, [Validators.min(0), Validators.max(150)]], });

onSubmit() { if (this.form.valid) { const value = this.form.getRawValue(); // Typed! console.log(value.name); // string, not string | null } } }

FormArray

form = this.fb.group({ name: ['', Validators.required], addresses: this.fb.array([this.createAddress()]), });

createAddress() { return this.fb.group({ street: ['', Validators.required], city: ['', Validators.required], zip: ['', [Validators.required, Validators.pattern(/^\d{5}$/)]], }); }

get addresses() { return this.form.get('addresses') as FormArray; }

addAddress() { this.addresses.push(this.createAddress()); }

removeAddress(index: number) { this.addresses.removeAt(index); }

Custom Validators

import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';

// Sync validator export function forbiddenName(name: string): ValidatorFn { return (control: AbstractControl): ValidationErrors | null => { const forbidden = control.value === name; return forbidden ? { forbiddenName: { value: control.value } } : null; }; }

// Async validator export function uniqueEmail(userService: UserService): AsyncValidatorFn { return (control: AbstractControl): Observable<ValidationErrors | null> => { return userService.checkEmail(control.value).pipe( map(exists => exists ? { emailTaken: true } : null), catchError(() => of(null)) ); }; }

// Cross-field validator export const passwordMatchValidator: ValidatorFn = (form: AbstractControl) => { const password = form.get('password')?.value; const confirm = form.get('confirmPassword')?.value; return password === confirm ? null : { passwordMismatch: true }; };

Error Display Pattern

@Component({ template: &#x3C;input formControlName="email" /> @if (form.controls.email.hasError('required') &#x26;&#x26; form.controls.email.touched) { &#x3C;span class="error">Email is required&#x3C;/span> } @if (form.controls.email.hasError('email') &#x26;&#x26; form.controls.email.touched) { &#x3C;span class="error">Invalid email format&#x3C;/span> } })

Anti-Patterns

Anti-Pattern Why It's Bad Correct Approach

Template-driven for complex forms Hard to test, no type safety Use reactive forms

Not using nonNullable

Nullable types everywhere Use fb.nonNullable.group()

Validation in component Not reusable Extract to validator functions

Subscribing to valueChanges without cleanup Memory leaks Use takeUntilDestroyed()

Quick Troubleshooting

Issue Likely Cause Solution

Form always invalid Missing required field value Check initial values

Type errors with controls Using untyped FormGroup Use fb.nonNullable.group()

Async validator fires too often No debounce Add updateOn: 'blur'

FormArray changes not reflected Missing trackBy Use track in @for

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

angular-forms

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

cron-scheduling

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

token-optimization

No summary provided by upstream source.

Repository SourceNeeds Review