Skip to content

Commit

Permalink
Merge branch 'feature-redesign'
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyang041060120 committed Aug 9, 2016
2 parents a789cab + 7895aeb commit 462c190
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 40 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,28 @@ export class AppComponent implements OnInit {
### rangeLength

```html
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" rangeLength="[5, 9]"/>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" [rangeLength]="[5, 9]"/>
<p *ngIf="field.errors?.rangeLength">error message</p>
```

### min

```html
<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" min="10"/>
<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" [min]="10"/>
<p *ngIf="field.errors?.min">error message</p>
```

### max

```html
<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" max="20"/>
<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" [max]="20"/>
<p *ngIf="field.errors?.max">error message</p>
```

### range

```html
<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" range="[10, 20]"/>
<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" [range]="[10, 20]"/>
<p *ngIf="field.errors?.range">error message</p>
```

Expand Down Expand Up @@ -167,7 +167,7 @@ export class AppComponent implements OnInit {
### phone

```html
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" phone="zh-CN"/>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" [phone]="'zh-CN'"/>
<p *ngIf="field.errors?.phone">error message</p>
```

Expand Down Expand Up @@ -195,7 +195,7 @@ export class AppComponent implements OnInit {
### uuid

```html
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" uuid="3"/>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" [uuid]="'all'"/>
<p *ngIf="field.errors?.uuid">error message</p>
```

Expand All @@ -211,7 +211,7 @@ export class AppComponent implements OnInit {
### equal

```html
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" equal="xxx"/>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" [equal]="'xxx'"/>
<p *ngIf="field.errors?.equal">error message</p>
```

Expand Down
1 change: 1 addition & 0 deletions example/src/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { CUSTOM_FORM_DIRECTIVES } from '../../src';
directives: [REACTIVE_FORM_DIRECTIVES, CUSTOM_FORM_DIRECTIVES]
})
export class AppComponent implements OnInit {
rangeLength = [5, 6];

constructor() {
}
Expand Down
20 changes: 16 additions & 4 deletions example/src/app.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
<form #form="ngForm" (ngSubmit)="onSubmit(form)" novalidate>
<h4>rangeLength</h4>
<input type="text" ngModel name="username" #username="ngModel" rangeLength="[4,6]" />
<input type="text" ngModel name="username" #username="ngModel" [rangeLength]="[5, 9]" />
<p *ngIf="username?.errors?.rangeLength">rangeLength error</p>

<h4>min</h4>
<input type="text" ngModel name="min" #min="ngModel" [min]="5" />
<p *ngIf="min?.errors?.min">min error</p>

<h4>max</h4>
<input type="text" ngModel name="max" #max="ngModel" [max]="9" />
<p *ngIf="max?.errors?.max">max error</p>

<h4>range</h4>
<input type="text" ngModel name="range" #range="ngModel" [range]="[5, 9]" />
<p *ngIf="range?.errors?.range">range error</p>

<h4>creditCard</h4>
<input type="text" ngModel name="creditCard" #creditCard="ngModel" creditCard />
<p *ngIf="creditCard?.errors?.creditCard">creditCard error</p>
Expand All @@ -16,14 +28,14 @@ <h4>base64</h4>
<p *ngIf="base?.errors?.base64">base64 error</p>

<h4>phone</h4>
<input type="text" ngModel name="phone" #phone="ngModel" phone="zh-CN" />
<input type="text" ngModel name="phone" #phone="ngModel" [phone]="'zh-CN'" />
<p *ngIf="phone?.errors?.phone">phone error</p>

<h4>UUID</h4>
<input type="text" ngModel name="uuid" #uuid="ngModel" uuid />
<input type="text" ngModel name="uuid" #uuid="ngModel" [uuid]="4" />
<p *ngIf="uuid?.errors?.uuid">UUID error</p>

<h4>equal to "xxx"</h4>
<input type="text" ngModel name="equal" #equal="ngModel" equal="xxx" />
<input type="text" ngModel name="equal" #equal="ngModel" [equal]="'xxx'" />
<p *ngIf="equal?.errors?.equal">equal error</p>
</form>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng2-validation",
"version": "1.2.13",
"version": "1.3.0",
"description": "angular2 validation",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
10 changes: 6 additions & 4 deletions src/directives/equal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Directive, Attribute, forwardRef } from '@angular/core';
import { Directive, Input, forwardRef, OnInit } from '@angular/core';
import { NG_VALIDATORS, Validator, ValidatorFn, AbstractControl } from '@angular/forms';

import { CustomValidators } from '../';
Expand All @@ -13,11 +13,13 @@ const EQUAL_VALIDATOR: any = {
selector: '[equal][formControlName],[equal][formControl],[equal][ngModel]',
providers: [EQUAL_VALIDATOR]
})
export class EqualValidator implements Validator {
export class EqualValidator implements Validator, OnInit {
@Input() equal: string;

private validator: ValidatorFn;

constructor(@Attribute('equal') equal: string) {
this.validator = CustomValidators.equal(equal);
ngOnInit() {
this.validator = CustomValidators.equal(this.equal);
}

validate(c: AbstractControl): {[key: string]: any} {
Expand Down
10 changes: 6 additions & 4 deletions src/directives/max.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Directive, Attribute, forwardRef } from '@angular/core';
import { Directive, Input, forwardRef, OnInit } from '@angular/core';
import { NG_VALIDATORS, Validator, ValidatorFn, AbstractControl } from '@angular/forms';

import { CustomValidators } from '../';
Expand All @@ -13,11 +13,13 @@ const MAX_VALIDATOR: any = {
selector: '[max][formControlName],[max][formControl],[max][ngModel]',
providers: [MAX_VALIDATOR]
})
export class MaxValidator implements Validator {
export class MaxValidator implements Validator, OnInit {
@Input() max: number;

private validator: ValidatorFn;

constructor(@Attribute('max') max: string) {
this.validator = CustomValidators.max(parseFloat(max));
ngOnInit() {
this.validator = CustomValidators.max(this.max);
}

validate(c: AbstractControl): {[key: string]: any} {
Expand Down
10 changes: 6 additions & 4 deletions src/directives/min.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Directive, Attribute, forwardRef } from '@angular/core';
import { Directive, Input, forwardRef, OnInit } from '@angular/core';
import { NG_VALIDATORS, Validator, ValidatorFn, AbstractControl } from '@angular/forms';

import { CustomValidators } from '../';
Expand All @@ -13,11 +13,13 @@ const MIN_VALIDATOR: any = {
selector: '[min][formControlName],[min][formControl],[min][ngModel]',
providers: [MIN_VALIDATOR]
})
export class MinValidator implements Validator {
export class MinValidator implements Validator, OnInit {
@Input() min: number;

private validator: ValidatorFn;

constructor(@Attribute('min') min: string) {
this.validator = CustomValidators.min(parseFloat(min));
ngOnInit() {
this.validator = CustomValidators.min(this.min);
}

validate(c: AbstractControl): {[key: string]: any} {
Expand Down
10 changes: 6 additions & 4 deletions src/directives/phone.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Directive, Attribute, forwardRef } from '@angular/core';
import { Directive, Input, forwardRef, OnInit } from '@angular/core';
import { NG_VALIDATORS, Validator, ValidatorFn, AbstractControl } from '@angular/forms';

import { CustomValidators } from '../';
Expand All @@ -13,11 +13,13 @@ const PHONE_VALIDATOR: any = {
selector: '[phone][formControlName],[phone][formControl],[phone][ngModel]',
providers: [PHONE_VALIDATOR]
})
export class PhoneValidator implements Validator {
export class PhoneValidator implements Validator, OnInit {
@Input() phone: string;

private validator: ValidatorFn;

constructor(@Attribute('phone') phone: string) {
this.validator = CustomValidators.phone(phone);
ngOnInit() {
this.validator = CustomValidators.phone(this.phone);
}

validate(c: AbstractControl): {[key: string]: any} {
Expand Down
10 changes: 6 additions & 4 deletions src/directives/range-length.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Directive, Attribute, forwardRef } from '@angular/core';
import { Directive, Input, forwardRef, OnInit } from '@angular/core';
import { NG_VALIDATORS, Validator, ValidatorFn, AbstractControl } from '@angular/forms';

import { CustomValidators } from '../';
Expand All @@ -13,11 +13,13 @@ const RANGE_LENGTH_VALIDATOR: any = {
selector: '[rangeLength][formControlName],[rangeLength][formControl],[rangeLength][ngModel]',
providers: [RANGE_LENGTH_VALIDATOR]
})
export class RangeLengthValidator implements Validator {
export class RangeLengthValidator implements Validator,OnInit {
@Input() rangeLength: [number];

private validator: ValidatorFn;

constructor(@Attribute('rangeLength') rangeLength: string) {
this.validator = CustomValidators.rangeLength(JSON.parse(rangeLength));
ngOnInit() {
this.validator = CustomValidators.rangeLength(this.rangeLength);
}

validate(c: AbstractControl): {[key: string]: any} {
Expand Down
10 changes: 6 additions & 4 deletions src/directives/range.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Directive, Attribute, forwardRef } from '@angular/core';
import { Directive, Input, forwardRef, OnInit } from '@angular/core';
import { NG_VALIDATORS, Validator, ValidatorFn, AbstractControl } from '@angular/forms';

import { CustomValidators } from '../';
Expand All @@ -13,11 +13,13 @@ const RANGE_VALIDATOR: any = {
selector: '[range][formControlName],[range][formControl],[range][ngModel]',
providers: [RANGE_VALIDATOR]
})
export class RangeValidator implements Validator {
export class RangeValidator implements Validator, OnInit {
@Input() range: [number];

private validator: ValidatorFn;

constructor(@Attribute('range') range: string) {
this.validator = CustomValidators.range(JSON.parse(range));
ngOnInit() {
this.validator = CustomValidators.range(this.range);
}

validate(c: AbstractControl): {[key: string]: any} {
Expand Down
10 changes: 6 additions & 4 deletions src/directives/uuid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Directive, Attribute, forwardRef } from '@angular/core';
import { Directive, Input, forwardRef, OnInit } from '@angular/core';
import { NG_VALIDATORS, Validator, ValidatorFn, AbstractControl } from '@angular/forms';

import { CustomValidators } from '../';
Expand All @@ -13,11 +13,13 @@ const UUID_VALIDATOR: any = {
selector: '[uuid][formControlName],[uuid][formControl],[uuid][ngModel]',
providers: [UUID_VALIDATOR]
})
export class UUIDValidator implements Validator {
export class UUIDValidator implements Validator, OnInit {
@Input() uuid;

private validator: ValidatorFn;

constructor(@Attribute('uuid') uuid: string) {
this.validator = CustomValidators.uuid(uuid);
ngOnInit() {
this.validator = CustomValidators.uuid(this.uuid);
}

validate(c: AbstractControl): {[key: string]: any} {
Expand Down

0 comments on commit 462c190

Please sign in to comment.