mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-11 20:13:07 +00:00
18 lines
608 B
TypeScript
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 } }
|
|
};
|
|
}
|