24 lines
882 B
TypeScript
24 lines
882 B
TypeScript
import {Directive, Input} from '@angular/core';
|
|
import { AbstractControl, NG_VALIDATORS, ValidationErrors, Validator, ValidatorFn } from '@angular/forms';
|
|
|
|
|
|
export function rangeRequired( enabled:boolean): ValidatorFn {
|
|
return (control: AbstractControl): ValidationErrors | null => {
|
|
const yearFrom = control.get('yearFrom');
|
|
const yearTo = control.get('yearTo');
|
|
return ((yearFrom && yearTo && enabled && (yearFrom.value == "" || yearTo.value == "")) ? { 'rangeRequired': true } : null);
|
|
};
|
|
}
|
|
|
|
|
|
@Directive({
|
|
selector: '[rangeRequired]',
|
|
providers: [{ provide: NG_VALIDATORS, useExisting: RangeYearsRequiredDirective, multi: true }]
|
|
})
|
|
export class RangeYearsRequiredDirective implements Validator {
|
|
@Input('rangeRequired') enabled:boolean = false;
|
|
validate(control: AbstractControl): ValidationErrors {
|
|
return rangeRequired(this.enabled)(control)
|
|
}
|
|
}
|