1 import {Directive, forwardRef, Injectable} from '@angular/core';
2 import {NG_ASYNC_VALIDATORS, AsyncValidator, FormControl} from '@angular/forms';
3 import {of} from 'rxjs';
4 import {switchMap, catchError} from 'rxjs/operators';
5 import {PcrudService} from '@eg/core/pcrud.service';
6 import {BookingModule} from './booking.module';
8 @Injectable({providedIn: BookingModule})
9 export class BookingResourceBarcodeValidator implements AsyncValidator {
11 private pcrud: PcrudService) {
14 validate = (control: FormControl) => {
15 return this.pcrud.search('brsrc',
16 {'barcode' : control.value},
18 switchMap(() => of(null)),
20 return of({ resourceBarcode: 'No resource found with that barcode' });
26 selector: '[egValidBookingResourceBarcode]',
28 provide: NG_ASYNC_VALIDATORS,
29 useExisting: forwardRef(() => BookingResourceBarcodeValidator),
33 export class BookingResourceBarcodeValidatorDirective {
35 private validator: BookingResourceBarcodeValidator
38 validate = (control: FormControl) => {
39 this.validator.validate(control);