Project

General

Profile

1
import { Directive, Input, OnChanges, SimpleChanges } from '@angular/core';
2
import { AbstractControl, NG_VALIDATORS, Validator, ValidatorFn, Validators } from '@angular/forms';
3
import { Dates } from "../string-utils.class";
4

    
5
export function inValidYearValidator(minYear, maxYear): ValidatorFn {
6
  return (control: AbstractControl): {[key: string]: any} | null => {
7
    return ((control.value && !Dates.isValidYear(control.value, minYear, maxYear)) ? {'inValidYear': {value: control.value}} : null);
8
  };
9
}
10

    
11
@Directive({
12
  selector: '[inValidYear]',
13
  providers: [{provide: NG_VALIDATORS, useExisting: InValidYearValidatorDirective, multi: true}]
14
})
15
export class InValidYearValidatorDirective implements Validator {
16
  @Input() maxYear = Dates.yearMax;
17
  @Input() minYear = Dates.yearMin;
18
  validate(control: AbstractControl): {[key: string]: any} | null {
19
    return inValidYearValidator(this.minYear, this.maxYear)(control);
20
  }
21
}
(2-2/7)