Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added gtTo #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion example/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@
</div>
</div>

<div class="form-group">
<label class="col-xs-3 control-label">greater than to</label>

<div class="col-xs-9">
<input class="form-control" type="text" ngModel name="gtTo0" #gtToNumber="ngModel"/>
<input class="form-control" type="text" ngModel name="gtTo1" #gtTo1="ngModel" [gtTo]="gtToNumber"/>
<p class="alert alert-danger" *ngIf="gtTo1?.errors?.gtTo">gtTo error</p>
</div>
</div>


<div class="form-group">
<label class="col-xs-3 control-label">less than</label>

Expand Down Expand Up @@ -263,4 +274,4 @@
</div>
</div>
</form>
</div>
</div>
1 change: 1 addition & 0 deletions src/greater-than-to/directive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// todo
28 changes: 28 additions & 0 deletions src/greater-than-to/directive.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions src/greater-than-to/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './directive';
export * from './validator';
14 changes: 14 additions & 0 deletions src/greater-than-to/validator.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
18 changes: 18 additions & 0 deletions src/greater-than-to/validator.ts
Original file line number Diff line number Diff line change
@@ -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};
};
};
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -37,6 +38,7 @@ export const CustomValidators: any = {
equalTo,
gt,
gte,
gtTo,
json,
lt,
lte,
Expand Down Expand Up @@ -65,6 +67,7 @@ const CUSTOM_FORM_DIRECTIVES = [
EqualToValidator,
GreaterThanValidator,
GreaterThanEqualValidator,
GreaterThanToValidator,
JSONValidator,
LessThanValidator,
LessThanEqualValidator,
Expand Down