1
|
import {Directive, Input} from '@angular/core';
|
2
|
import { AbstractControl, NG_VALIDATORS, ValidationErrors, Validator, ValidatorFn } from '@angular/forms';
|
3
|
|
4
|
|
5
|
export function rangeRequired( enabled:boolean): ValidatorFn {
|
6
|
return (control: AbstractControl): ValidationErrors | null => {
|
7
|
const yearFrom = control.get('yearFrom');
|
8
|
const yearTo = control.get('yearTo');
|
9
|
return ((yearFrom && yearTo && enabled && (yearFrom.value == "" || yearTo.value == "")) ? { 'rangeRequired': true } : null);
|
10
|
};
|
11
|
}
|
12
|
|
13
|
|
14
|
@Directive({
|
15
|
selector: '[rangeRequired]',
|
16
|
providers: [{ provide: NG_VALIDATORS, useExisting: RangeYearsRequiredDirective, multi: true }]
|
17
|
})
|
18
|
export class RangeYearsRequiredDirective implements Validator {
|
19
|
@Input('rangeRequired') enabled:boolean = false;
|
20
|
validate(control: AbstractControl): ValidationErrors {
|
21
|
return rangeRequired(this.enabled)(control)
|
22
|
}
|
23
|
}
|