Form control value validators

This commit is contained in:
BoykoAlex
2017-08-30 21:36:25 -04:00
parent dcfb5101af
commit 80f1511462

View File

@@ -1,7 +1,8 @@
import { dia } from 'jointjs';
import { ValidatorFn, AsyncValidatorFn } from '@angular/forms'
import { ValidatorFn, AsyncValidatorFn, AbstractControl, ValidationErrors } from '@angular/forms'
import { Flo } from './../shared/flo.common';
import { Subject } from 'rxjs/Subject'
import { Observable } from 'rxjs/Observable';
export namespace Properties {
@@ -177,4 +178,36 @@ export namespace Properties {
}
export namespace Validators {
export function uniqueResource(service : (value : any) => Observable<any>, debounce : number): AsyncValidatorFn {
return (control: AbstractControl): Observable<ValidationErrors> => {
return new Observable(obs => {
if (control.valueChanges && control.value) {
control.valueChanges
.debounceTime(debounce)
.flatMap(value => service(value))
.subscribe(() => {
obs.next({uniqueResource: true});
obs.complete();
}, () => {
obs.next(null);
obs.complete();
})
} else {
obs.next(null);
obs.complete();
}
});
}
}
export function noneOf(excluded : Array<any>) : ValidatorFn {
return (control: AbstractControl) : {[key: string]: any} => {
return excluded.find(e => e === control.value) ? {'noneOf': {value: control.value}} : null;
};
}
}
}