OnlineSalesAutoCrop/App/ClientApp/src/app/customValidators.ts

58 lines
1.3 KiB
TypeScript
Raw Normal View History

2026-06-14 12:46:29 +06:00
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);
}
}
}