2021-05-11 13:34:23 +02:00
|
|
|
// Core imports
|
2021-05-10 18:15:25 +02:00
|
|
|
import { AbstractControl, ValidationErrors } from '@angular/forms';
|
2020-12-28 10:06:30 +01:00
|
|
|
|
2021-05-11 13:34:23 +02:00
|
|
|
/**
|
|
|
|
* Provides methods to perform custom validation to form inputs.
|
|
|
|
*/
|
2020-12-28 10:06:30 +01:00
|
|
|
export class CustomValidator {
|
2021-05-11 13:34:23 +02:00
|
|
|
/**
|
|
|
|
* Sets errors to the confirm password input field if it does not match with the value in the password input field.
|
|
|
|
*
|
|
|
|
* @param control - The control object of the form being validated.
|
|
|
|
*/
|
2020-12-28 10:06:30 +01:00
|
|
|
static passwordMatchValidator(control: AbstractControl): void {
|
|
|
|
const password: string = control.get('password').value;
|
|
|
|
const confirmPassword: string = control.get('confirmPassword').value;
|
|
|
|
if (password !== confirmPassword) {
|
|
|
|
control.get('confirmPassword').setErrors({ NoPasswordMatch: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 13:34:23 +02:00
|
|
|
/**
|
|
|
|
* Sets errors to a form field if it does not match with the regular expression given.
|
|
|
|
*
|
|
|
|
* @param regex - The regular expression to match with the form field.
|
|
|
|
* @param error - Defines the map of errors to return from failed validation checks.
|
|
|
|
* @returns The map of errors returned from failed validation checks.
|
|
|
|
*/
|
2020-12-28 10:06:30 +01:00
|
|
|
static patternValidator(regex: RegExp, error: ValidationErrors): ValidationErrors | null {
|
|
|
|
return (control: AbstractControl): { [key: string]: any } => {
|
|
|
|
if (!control.value) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-04-29 19:10:39 +02:00
|
|
|
const valid: boolean = regex.test(control.value);
|
2020-12-28 10:06:30 +01:00
|
|
|
return valid ? null : error;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|