mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-18 07:23:03 +00:00
59334: edit item metadata finished
This commit is contained in:
32
src/app/shared/utils/in-list-validator.directive.ts
Normal file
32
src/app/shared/utils/in-list-validator.directive.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { Directive, Input } from '@angular/core';
|
||||
import { FormControl, NG_VALIDATORS, ValidationErrors, Validator } from '@angular/forms';
|
||||
import { inListValidator } from './validator.functions';
|
||||
|
||||
/**
|
||||
* Directive for validating if a ngModel value is in a given list
|
||||
*/
|
||||
@Directive({
|
||||
selector: '[ngModel][dsInListValidator]',
|
||||
// We add our directive to the list of existing validators
|
||||
providers: [
|
||||
{ provide: NG_VALIDATORS, useExisting: InListValidator, multi: true }
|
||||
]
|
||||
})
|
||||
export class InListValidator implements Validator {
|
||||
/**
|
||||
* The list to look in
|
||||
*/
|
||||
@Input()
|
||||
dsInListValidator: string[];
|
||||
|
||||
/**
|
||||
* The function that checks if the form control's value is currently valid
|
||||
* @param c The FormControl
|
||||
*/
|
||||
validate(c: FormControl): ValidationErrors | null {
|
||||
if (this.dsInListValidator !== null) {
|
||||
return inListValidator(this.dsInListValidator)(c);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -1,7 +1,11 @@
|
||||
import { AbstractControl, ValidatorFn } from '@angular/forms';
|
||||
|
||||
/**
|
||||
* 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 contains = list.indexOf(control.value) > 0;
|
||||
const contains = list.indexOf(control.value) > -1;
|
||||
return contains ? null : {inList: {value: control.value}} };
|
||||
}
|
||||
|
Reference in New Issue
Block a user