openaire-library/utils/rangeFilter/inValidYear.directive.ts

22 lines
931 B
TypeScript

import { Directive, Input, OnChanges, SimpleChanges } from '@angular/core';
import { AbstractControl, NG_VALIDATORS, Validator, ValidatorFn, Validators } from '@angular/forms';
import { Dates } from "../string-utils.class";
export function inValidYearValidator(minYear, maxYear): ValidatorFn {
return (control: AbstractControl): {[key: string]: any} | null => {
return ((control.value && !Dates.isValidYear(control.value, minYear, maxYear)) ? {'inValidYear': {value: control.value}} : null);
};
}
@Directive({
selector: '[inValidYear]',
providers: [{provide: NG_VALIDATORS, useExisting: InValidYearValidatorDirective, multi: true}]
})
export class InValidYearValidatorDirective implements Validator {
@Input() maxYear = Dates.yearMax;
@Input() minYear = Dates.yearMin;
validate(control: AbstractControl): {[key: string]: any} | null {
return inValidYearValidator(this.minYear, this.maxYear)(control);
}
}