openaire-library/utils/rangeFilter/fromYearAfterToYear.directi...

20 lines
843 B
TypeScript
Raw Normal View History

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)
}
}