Project

General

Profile

1 61381 k.triantaf
import { Directive } from '@angular/core';
2
import { AbstractControl, FormGroup, NG_VALIDATORS, ValidationErrors, Validator, ValidatorFn } from '@angular/forms';
3
4
export const fromYearAfterToYearValidator: ValidatorFn = (control: FormGroup): ValidationErrors | null => {
5
  const yearFrom = control.get('yearFrom');
6
  const yearTo = control.get('yearTo');
7
8
  return ((yearFrom && yearTo && (parseInt(yearFrom.value, 10) >  parseInt(yearTo.value, 10))) ? { 'fromYearAfterToYear': true } : null);
9
};
10
11
@Directive({
12
  selector: '[fromYearAfterToYear]',
13
  providers: [{ provide: NG_VALIDATORS, useExisting: FromYearAfterToYearValidatorDirective, multi: true }]
14
})
15
export class FromYearAfterToYearValidatorDirective implements Validator {
16
  validate(control: AbstractControl): ValidationErrors {
17
    return fromYearAfterToYearValidator(control)
18
  }
19
}