Files
dspace-angular/src/app/shared/utils/validator.functions.ts
2019-02-25 09:57:46 +01:00

18 lines
608 B
TypeScript

import { AbstractControl, ValidatorFn } from '@angular/forms';
import { isNotEmpty } from '../empty.util';
/**
* Returns a validator function to check if the control's value is in a given list
* @param list The list to look in
*/
export function inListValidator(list: string[]): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } | null => {
const hasValue = isNotEmpty(control.value);
let inList = true;
if (isNotEmpty(list)) {
inList = list.indexOf(control.value) > -1;
}
return (hasValue && inList) ? null : { inList: { value: control.value } }
};
}