Angular Forms
ASecurityUse a standalone `ValidatorFn` in `validators.ts`, then attach it to a strictly typed reactive form. ```ts // validators.ts import { AbstractControl, ValidationErrors, ValidatorFn, } from '@angular/forms'; export function forbiddenName(name: string): ValidatorFn { return (control: AbstractControl): ValidationErrors | null => { const value = String(control.value ?? '').trim().toLowerCase(); return value === name.toLowerCase() ? { forbiddenName: { value: control.value } } : null; }; } ``` ```ts...
- 584 stars
- 0 votes
- 0 copies
- 3 views
- Added September 5, 2026
Security analysis
100/100Pro scans all 18 files and shows the line behind each finding
npx -y skills add HoangNguyen0403/agent-skills-standard --skill angular-forms --agent claude-codeAre you the author of Angular Forms?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-angular-forms-da7de313)Use a standalone `ValidatorFn` in `validators.ts`, then attach it to a strictly typed reactive form.
```ts
// validators.ts
import {
AbstractControl,
ValidationErrors,
ValidatorFn,
} from '@angular/forms';
export function forbiddenName(name: string): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const value = String(control.value ?? '').trim().toLowerCase();
return value === name.toLowerCase()
? { forbiddenName: { value: control.value } }
: null;
};
}
```
```ts
// profile.component.ts
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { forbiddenName } from './validators';
interface ProfileForm {
username: string;
email: string;
}
export class ProfileComponent {
readonly form: FormGroup<ProfileForm>;
constructor(private readonly fb: FormBuilder) {
this.form = this.fb.nonNullable.group({
username: ['', [Validators.required, forbiddenName('admin')]],
email: ['', [Validators.required, Validators.email]],
});
}
submit(): void {
if (this.form.invalid) {
this.form.markAllAsTouched();
return;
}
console.log(this.form.getRawValue());
}
}
```
Display the custom error in the template:
```html
<input formControlName="username" />
@if (form.controls.username.hasError('forbiddenName')) {
<p>This username is not allowed.</p>
}
```
`null` means valid; returning an object such as `{ forbiddenName: ... }` makes the control invalid. This uses Reactive Forms, `FormGroup<T>`, `fb.nonNullable.group(...)`, and keeps validation logic outside the component class.
Files in this skill
- eval-1.baseline.md
- eval-1.with-skill.md
- eval-2.baseline.md
- eval-2.with-skill.md
- eval-3.baseline.md
- eval-3.with-skill.md
- eval-4.baseline.md
- eval-4.with-skill.md
- eval-5.baseline.md
- eval-5.with-skill.md
- eval-6.baseline.md
- eval-6.with-skill.md
- trigger-1.md
- trigger-2.md
- trigger-3.md
- trigger-4.md
- trigger-5.md
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.