Explain it simply
Custom Validators, in plain language
You can create custom validators for your application's specific requirements.
Problem → Why → Solution
The problem — why this concept matters
Mutating control values inside a validator can trigger recursive updates and unpredictable state.
A common mistake
One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.
Core concept — understand how it works
A custom validator should be a pure function that returns null for valid input or a stable error object for invalid input. Error keys and metadata separate validation logic from UI messages, and factories create configurable rules. Async validators need cancellation or debouncing, pending state, and a distinction between validation failure and server failure.
- input and state
- component and template
- rendered Angular UI
Explain the core concept and Angular data flow of Custom Validators
Core syntax or pattern
// custom-validators.ts
import { AbstractControl, ValidationErrors } from '@angular/forms';
export class CustomValidators {
// No whitespace validator
static noWhitespace(control: AbstractControl): ValidationErrors | null {
const isWhitespace = (control.value || '').trim().length === 0;
return isWhitespace ? { whitespace: true } : null;
}
// Password match validator
static passwordMatch(control: AbstractControl): ValidationErrors | null {
const password = control.get('password');
const confirmPassword = control.get('confirmPassword');
if (!password || !confirmPassword) return null;
return password.value === confirmPassword.value
? null
: { passwordMismatch: true };
}
}
// Usage in component
this.userForm = this.fb.group({
username: ['', [Validators.required, CustomValidators.noWhitespace]],
password: ['', Validators.required],
confirmPassword: ['']
}, { validators: CustomValidators.passwordMatch });A custom validator should be a pure function that returns null for valid input or a stable error object for invalid input. Error keys and metadata separate validation logic from UI messages, and factories create configurable rules. Async validators need cancellation or debouncing, pending state, and a distinction between validation failure and server failure.
Read the code step by step
- input and state
// custom-validators.tsYou can create custom validators for your application's specific requirements.
- component and template
if (!password || !confirmPassword) return null;A custom validator should be a pure function that returns null for valid input or a stable error object for invalid input. Error keys and metadata separate validation logic from UI messages, and factories create configurable rules. Async validators need cancellation or debouncing, pending state, and a distinction between validation failure and server failure.
- rendered Angular UI
}, { validators: CustomValidators.passwordMatch });Write a password-match group validator and a reserved-username async validator, unit-testing valid, invalid, empty, and server-failure cases.
- input and state
- component and template
- rendered Angular UI
Success
Explain the core concept and Angular data flow of Custom Validators
Failure
Mutating control values inside a validator can trigger recursive updates and unpredictable state.
You can create custom validators for your application's specific requirements.
// custom-validators.ts
import { AbstractControl, ValidationErrors } from '@angular/forms';
export class CustomValidators {
// No whitespace validator
static noWhitespace(control: AbstractControl): ValidationErrors | null {
const isWhitespace = (control.value || '').trim().length === 0;
return isWhitespace ? { whitespace: true } : null;
}
// Password match validator
static passwordMatch(control: AbstractControl): ValidationErrors | null {
const password = control.get('password');
const confirmPassword = control.get('confirmPassword');
if (!password || !confirmPassword) return null;
return password.value === confirmPassword.value
? null
: { passwordMismatch: true };
}
}
// Usage in component
this.userForm = this.fb.group({
username: ['', [Validators.required, CustomValidators.noWhitespace]],
password: ['', Validators.required],
confirmPassword: ['']
}, { validators: CustomValidators.passwordMatch });(The custom validation rules work and return their specific errors)Real-world usage
How to apply it in a real project
Write a password-match group validator and a reserved-username async validator, unit-testing valid, invalid, empty, and server-failure cases.
Mini challenge
Build it yourself
Rebuild the Custom Validators example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
// custom-validators.ts
import { AbstractControl, ValidationErrors } from '@angular/forms';
export class CustomValidators {
// No whitespace validator
static noWhitespace(control: AbstractControl): ValidationErrors | null {
const isWhitespace = (control.value || '').trim().length === 0;
return isWhitespace ? { whitespace: true } : null;
}
// Password match validator
static passwordMatch(control: AbstractControl): ValidationErrors | null {
const password = control.get('password');
const confirmPassword = control.get('confirmPassword');
if (!password || !confirmPassword) return null;
return password.value === confirmPassword.value
? null
: { passwordMismatch: true };
}
}
// Usage in component
this.userForm = this.fb.group({
username: ['', [Validators.required, CustomValidators.noWhitespace]],
password: ['', Validators.required],
confirmPassword: ['']
}, { validators: CustomValidators.passwordMatch });Expected: Explain the core concept and Angular data flow of Custom Validators
Show hint
Write a password-match group validator and a reserved-username async validator, unit-testing valid, invalid, empty, and server-failure cases.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- input and state → component and template → rendered Angular UI
- A custom validator should be a pure function that returns null for valid input or a stable error object for invalid input. Error keys and metadata separate validation logic from UI messages, and factories create configurable rules. Async validators need cancellation or debouncing, pending state, and a distinction between validation failure and server failure.
- Mutating control values inside a validator can trigger recursive updates and unpredictable state.
- Write a password-match group validator and a reserved-username async validator, unit-testing valid, invalid, empty, and server-failure cases.
Angular — Forms — Angular