33 lines
1.5 KiB
TypeScript
33 lines
1.5 KiB
TypeScript
import { AbstractControl, FormGroup, ValidatorFn, Validators } from '@angular/forms';
|
|
import { ValidationErrorModel } from './error-model/validation-error-model';
|
|
|
|
export function BackendErrorValidator(errorModel: ValidationErrorModel, propertyName: string): ValidatorFn {
|
|
return (control: AbstractControl): { [key: string]: any } => {
|
|
const error: String = errorModel.getError(propertyName);
|
|
return error ? { 'backendError': { message: error } } : null;
|
|
};
|
|
}
|
|
|
|
export function E164PhoneValidator(): ValidatorFn {
|
|
return Validators.pattern('^\\+?[1-9]\\d{1,14}$');
|
|
}
|
|
|
|
// Getter is required because the index of each element is not fixed (array does not always follow LIFO)
|
|
export function BackendArrayErrorValidator(errorModel: ValidationErrorModel, propertyNameGetter: () => string): ValidatorFn {
|
|
return (control: AbstractControl): { [key: string]: any } => {
|
|
const error: String = errorModel.getError(propertyNameGetter());
|
|
return error ? { 'backendError': { message: error } } : null;
|
|
};
|
|
}
|
|
|
|
export function CustomErrorValidator(errorModel: ValidationErrorModel, propertyNames: string[]): ValidatorFn {
|
|
return (control: AbstractControl): { [key: string]: any } => {
|
|
const error: String = errorModel.getErrors(propertyNames);
|
|
return error ? { 'customError': { message: error } } : null;
|
|
};
|
|
}
|
|
|
|
export function PasswordMatchValidator(formGroup: FormGroup) {
|
|
return formGroup.get('password').value === formGroup.get('passwordConfirm').value ? null : { 'passwordMismatch': true };
|
|
}
|