cic-staff-client/src/app/_helpers/custom.validator.ts
Spencer Ofwiti 5228842e61 Merge branch 'master' into spencer/docs
# Conflicts:
#	package-lock.json
#	package.json
#	src/app/_eth/accountIndex.ts
#	src/app/_eth/token-registry.ts
#	src/app/_guards/auth.guard.ts
#	src/app/_guards/role.guard.ts
#	src/app/_helpers/array-sum.ts
#	src/app/_helpers/clipboard-copy.ts
#	src/app/_helpers/custom-error-state-matcher.ts
#	src/app/_helpers/custom.validator.ts
#	src/app/_helpers/export-csv.ts
#	src/app/_helpers/global-error-handler.ts
#	src/app/_helpers/http-getter.ts
#	src/app/_helpers/mock-backend.ts
#	src/app/_helpers/read-csv.ts
#	src/app/_helpers/schema-validation.ts
#	src/app/_services/user.service.spec.ts
2021-05-11 20:41:47 +03:00

39 lines
1.3 KiB
TypeScript

// Core imports
import { AbstractControl, ValidationErrors } from '@angular/forms';
/**
* Provides methods to perform custom validation to form inputs.
*/
export class CustomValidator {
/**
* 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.
*/
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 });
}
}
/**
* 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.
*/
static patternValidator(regex: RegExp, error: ValidationErrors): ValidationErrors | null {
return (control: AbstractControl): { [key: string]: any } => {
if (!control.value) {
return null;
}
const valid: boolean = regex.test(control.value);
return valid ? null : error;
};
}
}