58 lines
1.3 KiB
TypeScript
58 lines
1.3 KiB
TypeScript
import { AbstractControl, ValidatorFn } from '@angular/forms';
|
|
|
|
export function EqualValidator(controlName: string, matchingControlName: string): ValidatorFn | null
|
|
{
|
|
return (formGroup: AbstractControl): { [key: string]: any } | null =>
|
|
{
|
|
const control = formGroup.get(controlName);
|
|
const matchingControl = formGroup.get(matchingControlName);
|
|
|
|
if (!control || !matchingControl)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (matchingControl.errors && !matchingControl.errors.confirmedValidator)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (control.value !== matchingControl.value)
|
|
{
|
|
matchingControl.setErrors({ equalValidator: true });
|
|
}
|
|
else
|
|
{
|
|
matchingControl.setErrors(null);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function UnequalValidator(controlName: string, matchingControlName: string): ValidatorFn | null
|
|
{
|
|
return (formGroup: AbstractControl): { [key: string]: any } | null =>
|
|
{
|
|
const control = formGroup.get(controlName);
|
|
const matchingControl = formGroup.get(matchingControlName);
|
|
|
|
if (!control || !matchingControl)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (matchingControl.errors && !matchingControl.errors.confirmedValidator)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (control.value === matchingControl.value)
|
|
{
|
|
matchingControl.setErrors({ unequalValidator: true });
|
|
}
|
|
else
|
|
{
|
|
matchingControl.setErrors(null);
|
|
}
|
|
}
|
|
}
|