import { AbstractControl, ValidatorFn, FormGroup } from '@angular/forms'; export const validateMinMax = (minControlName: string, maxControlName: string): ValidatorFn => (control: AbstractControl): { [key: string]: boolean } | null => { const minControl = control.get(minControlName); const maxControl = control.get(maxControlName); if (!minControl || !maxControl) { return null; } const minAmount = minControl.value; const maxAmount = maxControl.value; if (maxAmount !== null && minAmount !== null && (maxAmount < minAmount || minAmount > maxAmount)) { // Manually set error state on controls minControl.setErrors({ minMaxError: true }); maxControl.setErrors({ minMaxError: true }); return { minMaxError: true }; } // Manually clear error state if no validation error minControl.setErrors(null); maxControl.setErrors(null); return null; };