20 lines
843 B
TypeScript
20 lines
843 B
TypeScript
import { Directive } from '@angular/core';
|
|
import { AbstractControl, FormGroup, NG_VALIDATORS, ValidationErrors, Validator, ValidatorFn } from '@angular/forms';
|
|
|
|
export const fromYearAfterToYearValidator: ValidatorFn = (control: FormGroup): ValidationErrors | null => {
|
|
const yearFrom = control.get('yearFrom');
|
|
const yearTo = control.get('yearTo');
|
|
|
|
return ((yearFrom && yearTo && (parseInt(yearFrom.value, 10) > parseInt(yearTo.value, 10))) ? { 'fromYearAfterToYear': true } : null);
|
|
};
|
|
|
|
@Directive({
|
|
selector: '[fromYearAfterToYear]',
|
|
providers: [{ provide: NG_VALIDATORS, useExisting: FromYearAfterToYearValidatorDirective, multi: true }]
|
|
})
|
|
export class FromYearAfterToYearValidatorDirective implements Validator {
|
|
validate(control: AbstractControl): ValidationErrors {
|
|
return fromYearAfterToYearValidator(control)
|
|
}
|
|
}
|