]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/validators/not_before_moment_validator.directive.ts
Merge branch 'master' of git.evergreen-ils.org:Evergreen
[Evergreen.git] / Open-ILS / src / eg2 / src / app / share / validators / not_before_moment_validator.directive.ts
1 import {Directive, Input} from '@angular/core';
2 import {NG_VALIDATORS, AbstractControl, FormControl, ValidationErrors, ValidatorFn} from '@angular/forms';
3 import {Injectable} from '@angular/core';
4
5 import * as Moment from 'moment-timezone';
6
7 export function notBeforeMomentValidator(notBeforeMe: Moment): ValidatorFn {
8     return (control: AbstractControl): {[key: string]: any} | null => {
9         return (control.value && control.value.isBefore(notBeforeMe)) ?
10             {tooEarly: 'This cannot be before ' + notBeforeMe.format('LLL')} : null;
11     };
12 }
13
14 @Directive({
15     selector: '[egNotBeforeMoment]',
16     providers: [{
17         provide: NG_VALIDATORS,
18         useExisting: NotBeforeMomentValidatorDirective,
19         multi: true
20     }]
21 })
22 export class NotBeforeMomentValidatorDirective {
23     @Input('egNotBeforeMoment') notBeforeMoment: Moment;
24
25     validate(control: AbstractControl): {[key: string]: any} | null {
26         return this.notBeforeMoment ?
27             notBeforeMomentValidator(this.notBeforeMoment)(control)
28             : null;
29     }
30 }
31
32