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

19 lines
798 B
TypeScript
Raw Normal View History

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(): ValidatorFn {
return (control: AbstractControl): {[key: string]: any} | null => {
return ((control.value && !Dates.isValidYear(control.value)) ? {'inValidYear': {value: control.value}} : null);
};
}
@Directive({
selector: '[inValidYear]',
providers: [{provide: NG_VALIDATORS, useExisting: InValidYearValidatorDirective, multi: true}]
})
export class InValidYearValidatorDirective implements Validator {
validate(control: AbstractControl): {[key: string]: any} | null {
return inValidYearValidator()(control);
}
}