frontend/src/app/services/CustomValidators/custom-validators.service.ts
import { Injectable } from "@angular/core";
import {
ValidatorFn,
FormGroup,
FormControl,
FormGroupDirective,
NgForm
} from "@angular/forms";
import { ErrorStateMatcher } from "@angular/material/core";
// source: https://stackoverflow.com/a/48292166/5954839
@Injectable({
providedIn: "root"
})
export class CustomValidatorsService {
constructor() {}
/**
* Validates that a formGroup's children all have the same value
*/
static childrenEqual: ValidatorFn = (formGroup: FormGroup) => {
const [firstControlName, ...otherControlNames] = Object.keys(
formGroup.controls || {}
);
const isValid = otherControlNames.every(
controlName =>
formGroup.get(controlName).value ===
formGroup.get(firstControlName).value
);
return isValid ? null : { childrenNotEqual: true };
};
}
export class ConfirmValidParentMatcher implements ErrorStateMatcher {
isErrorState(
control: FormControl | null,
form: FormGroupDirective | NgForm | null
): boolean {
return control.parent.invalid && control.touched;
}
}
/**
* Collection of reusable error messages
*/
export const errorMessages: { [key: string]: string } = {
fullName: "Full name must be between 1 and 128 characters",
email: "Email must be a valid email address (username@domain)",
confirmEmail: "Email addresses must match",
password:
"Password must be between 7 and 15 characters, and contain at least one number and special character",
confirmPassword: "Passwords must match"
};