14 lines
449 B
TypeScript
14 lines
449 B
TypeScript
import { AbstractControl, ValidationErrors, ValidatorFn } from "@angular/forms";
|
|
|
|
|
|
export function createItemExistsInArrayValidator(items: any[], getter: (value: any) => any): ValidatorFn {
|
|
return (control: AbstractControl): ValidationErrors | null => {
|
|
const value = control.value;
|
|
|
|
if (!value)
|
|
return null;
|
|
|
|
const matches = items.some(i => getter(i) == value);
|
|
return matches ? { itemExistsInArray: true } : null;
|
|
}
|
|
} |