1 import {Directive, Input} from '@angular/core';
2 import {NG_VALIDATORS, AbstractControl, ValidatorFn} from '@angular/forms';
4 import * as moment from 'moment-timezone';
6 export function notBeforeMomentValidator(notBeforeMe: moment.Moment): ValidatorFn {
7 return (control: AbstractControl): {[key: string]: any} | null => {
8 return (control.value && control.value.isBefore(notBeforeMe)) ?
9 {tooEarly: 'This cannot be before ' + notBeforeMe.format('LLL')} : null;
14 selector: '[egNotBeforeMoment]',
16 provide: NG_VALIDATORS,
17 useExisting: NotBeforeMomentValidatorDirective,
21 export class NotBeforeMomentValidatorDirective {
22 @Input('egNotBeforeMoment') notBeforeMoment: moment.Moment;
24 validate(control: AbstractControl): {[key: string]: any} | null {
25 return this.notBeforeMoment ?
26 notBeforeMomentValidator(this.notBeforeMoment)(control)