From bb8831ab5da0c1e36c53d256c4287f94804fde03 Mon Sep 17 00:00:00 2001 From: petru Date: Wed, 24 May 2017 15:55:36 +0300 Subject: [PATCH] added gtTo --- example/src/app.html | 13 ++++++++++++- src/greater-than-to/directive.spec.ts | 1 + src/greater-than-to/directive.ts | 28 +++++++++++++++++++++++++++ src/greater-than-to/index.ts | 2 ++ src/greater-than-to/validator.spec.ts | 14 ++++++++++++++ src/greater-than-to/validator.ts | 18 +++++++++++++++++ src/index.ts | 3 +++ 7 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/greater-than-to/directive.spec.ts create mode 100644 src/greater-than-to/directive.ts create mode 100644 src/greater-than-to/index.ts create mode 100644 src/greater-than-to/validator.spec.ts create mode 100644 src/greater-than-to/validator.ts diff --git a/example/src/app.html b/example/src/app.html index 4103332..96fb52a 100644 --- a/example/src/app.html +++ b/example/src/app.html @@ -164,6 +164,17 @@ +
+ + +
+ + +

gtTo error

+
+
+ +
@@ -263,4 +274,4 @@
- \ No newline at end of file + diff --git a/src/greater-than-to/directive.spec.ts b/src/greater-than-to/directive.spec.ts new file mode 100644 index 0000000..65b3dba --- /dev/null +++ b/src/greater-than-to/directive.spec.ts @@ -0,0 +1 @@ +// todo diff --git a/src/greater-than-to/directive.ts b/src/greater-than-to/directive.ts new file mode 100644 index 0000000..d3f88d7 --- /dev/null +++ b/src/greater-than-to/directive.ts @@ -0,0 +1,28 @@ +import { Directive, Input, forwardRef, OnInit } from '@angular/core'; +import { NG_VALIDATORS, Validator, FormControl, ValidatorFn, AbstractControl } from '@angular/forms'; + +import { gtTo } from './'; + +const GREATER_THAN_TO_VALIDATOR: any = { + provide: NG_VALIDATORS, + useExisting: forwardRef(() => GreaterThanToValidator), + multi: true +}; + +@Directive({ + selector: '[gtTo][formControlName],[gtTo][formControl],[gtTo][ngModel]', + providers: [GREATER_THAN_TO_VALIDATOR] +}) +export class GreaterThanToValidator implements Validator, OnInit { + @Input() gtTo: FormControl; + + private validator: ValidatorFn; + + ngOnInit() { + this.validator = gtTo(this.gtTo); + } + + validate(c: AbstractControl): {[key: string]: any} { + return this.validator(c); + } +} diff --git a/src/greater-than-to/index.ts b/src/greater-than-to/index.ts new file mode 100644 index 0000000..5c8cea0 --- /dev/null +++ b/src/greater-than-to/index.ts @@ -0,0 +1,2 @@ +export * from './directive'; +export * from './validator'; diff --git a/src/greater-than-to/validator.spec.ts b/src/greater-than-to/validator.spec.ts new file mode 100644 index 0000000..f2a94b1 --- /dev/null +++ b/src/greater-than-to/validator.spec.ts @@ -0,0 +1,14 @@ +import { FormControl } from '@angular/forms'; + +import { gtTo } from './'; + +describe('GreatThanTo', () => { + const error = {equalTo: true}; + + it('5 should be gtTo to 4', () => { + let control = new FormControl(5); + let control1 = new FormControl(4); + + expect(gtTo(control1)(control)).toBeNull(); + }); +}); diff --git a/src/greater-than-to/validator.ts b/src/greater-than-to/validator.ts new file mode 100644 index 0000000..c021edc --- /dev/null +++ b/src/greater-than-to/validator.ts @@ -0,0 +1,18 @@ +import { AbstractControl, ValidatorFn } from '@angular/forms'; + +export const gtTo = (gtToControl: AbstractControl): ValidatorFn => { + let subscribe: boolean = false; + + return (control: AbstractControl): {[key: string]: boolean} => { + if (!subscribe) { + subscribe = true; + gtToControl.valueChanges.subscribe(() => { + control.updateValueAndValidity(); + }); + } + + let v: number = +control.value; + + return +gtToControl.value < v ? null : {gtTo: true}; + }; +}; diff --git a/src/index.ts b/src/index.ts index d088187..280004a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ import { equal, EqualValidator } from './equal'; import { equalTo, EqualToValidator } from './equal-to'; import { gt, GreaterThanValidator } from './greater-than'; import { gte, GreaterThanEqualValidator } from './greater-than-equal'; +import { gtTo, GreaterThanToValidator } from './greater-than-to'; import { json, JSONValidator } from './json'; import { lt, LessThanValidator } from './less-than'; import { lte, LessThanEqualValidator } from './less-than-equal'; @@ -37,6 +38,7 @@ export const CustomValidators: any = { equalTo, gt, gte, + gtTo, json, lt, lte, @@ -65,6 +67,7 @@ const CUSTOM_FORM_DIRECTIVES = [ EqualToValidator, GreaterThanValidator, GreaterThanEqualValidator, + GreaterThanToValidator, JSONValidator, LessThanValidator, LessThanEqualValidator,