]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/validators/not_before_moment_validator.directive.ts
LP1915464 follow-up: use spaces, not tabs; remove extra comma
[working/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, ValidatorFn} from '@angular/forms';
3
4 import * as moment from 'moment-timezone';
5
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;
10     };
11 }
12
13 @Directive({
14     selector: '[egNotBeforeMoment]',
15     providers: [{
16         provide: NG_VALIDATORS,
17         useExisting: NotBeforeMomentValidatorDirective,
18         multi: true
19     }]
20 })
21 export class NotBeforeMomentValidatorDirective {
22     @Input('egNotBeforeMoment') notBeforeMoment: moment.Moment;
23
24     validate(control: AbstractControl): {[key: string]: any} | null {
25         return this.notBeforeMoment ?
26             notBeforeMomentValidator(this.notBeforeMoment)(control)
27             : null;
28     }
29 }
30
31