-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from luisvt/add-equal-to
feat(equal-to): Add equal-to validator and directive
- Loading branch information
Showing
20 changed files
with
2,900 additions
and
1,207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
examples/src/app/equal-to-validator/equal-to-validator.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<mat-form-field> | ||
<mat-select placeholder="Type" [(value)]="selected"> | ||
<mat-option value="form" *ngIf="currentValidator.formHTML"> | ||
Form | ||
</mat-option> | ||
<mat-option value="reactiveForm" *ngIf="currentValidator.reactiveformHTML"> | ||
Reactive Form | ||
</mat-option> | ||
</mat-select> | ||
</mat-form-field> | ||
<mat-card class="card"> | ||
<mat-card-header> | ||
<mat-card-title>EqualTo validator</mat-card-title> | ||
</mat-card-header> | ||
<mat-card-content> | ||
<ng-container *ngIf="selected =='reactiveForm'"> | ||
<app-reactive-equal-to></app-reactive-equal-to> | ||
</ng-container> | ||
<ng-container *ngIf="selected =='form'"> | ||
<app-equal-to></app-equal-to> | ||
</ng-container> | ||
</mat-card-content> | ||
</mat-card> | ||
<app-codeviewer *ngIf="currentValidator && selected =='form'" [html]="currentValidator.formHTML" [tsCode]="currentValidator.formTS"></app-codeviewer> | ||
<app-codeviewer *ngIf="currentValidator && selected =='reactiveForm'" [html]="currentValidator.reactiveformHTML" [tsCode]="currentValidator.reactiveformTS"></app-codeviewer> |
32 changes: 32 additions & 0 deletions
32
examples/src/app/equal-to-validator/equal-to-validator.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Component, OnDestroy, OnInit } from '@angular/core'; | ||
import { equalTo, Validator } from '../items'; | ||
import { ActivatedRoute } from '@angular/router'; | ||
|
||
@Component({ | ||
selector: 'app-equals-to-field-validator', | ||
templateUrl: './equal-to-validator.component.html' | ||
}) | ||
export class EqualToValidatorComponent implements OnInit, OnDestroy { | ||
|
||
private sub: any; | ||
validatorItems = equalTo; | ||
item: string = this.validatorItems[0].name; | ||
currentValidator: Validator; | ||
selected = 'reactiveForm'; | ||
|
||
constructor(private route: ActivatedRoute) { } | ||
|
||
ngOnInit() { | ||
this.currentValidator = this.validatorItems.filter(x => x.name === this.item)[0]; | ||
this.sub = this.route.params.subscribe(params => { | ||
this.item = params['id']; | ||
if (this.item) { | ||
this.currentValidator = this.validatorItems.filter(x => x.name === this.item)[0]; | ||
} | ||
}); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.sub.unsubscribe(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
examples/src/app/equal-to-validator/equal-to-validator.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { EqualToValidatorComponent } from './equal-to-validator.component'; | ||
import { FormEqualToComponent } from './form/equal-to/form-equal-to.component'; | ||
import { RouterModule, Routes } from '@angular/router'; | ||
import { | ||
MatButtonModule, | ||
MatCardModule, | ||
MatFormFieldModule, | ||
MatInputModule, | ||
MatOptionModule, | ||
MatSelectModule | ||
} from '@angular/material'; | ||
import { UtilModule } from '../util/util.module'; | ||
import { FormsModule, ReactiveFormsModule } from '@angular/forms'; | ||
import { ValidatorsModule } from 'ngx-validators'; | ||
import { ReactiveFormEqualToComponent } from './reactive-form/equal-to/reactive-form-equal-to.component'; | ||
|
||
const routes: Routes = [ | ||
{path: 'equal-to', component: EqualToValidatorComponent}, | ||
{path: 'equal-to/:id', component: EqualToValidatorComponent} | ||
]; | ||
|
||
|
||
@NgModule({ | ||
imports: [ | ||
CommonModule, | ||
FormsModule, | ||
ReactiveFormsModule, | ||
RouterModule.forChild(routes), | ||
UtilModule, | ||
ValidatorsModule, | ||
MatFormFieldModule, | ||
MatInputModule, | ||
MatSelectModule, | ||
MatOptionModule, | ||
MatCardModule, | ||
MatButtonModule | ||
], | ||
declarations: [ | ||
EqualToValidatorComponent, | ||
FormEqualToComponent, | ||
ReactiveFormEqualToComponent | ||
] | ||
}) | ||
export class EqualToValidatorModule { } |
32 changes: 32 additions & 0 deletions
32
examples/src/app/equal-to-validator/form/equal-to/form-equal-to.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<form> | ||
<h3>Passing Field Name:</h3> | ||
<div class="row"> | ||
<mat-form-field class="col-6"> | ||
<mat-label>Email:</mat-label> | ||
<input type="email" matInput [(ngModel)]="model.email" name="email" #email="ngModel" required> | ||
<mat-error *ngIf="email.hasError('required')">This field is required</mat-error> | ||
</mat-form-field> | ||
<mat-form-field class="col-6"> | ||
<mat-label>Confirm Email:</mat-label> | ||
<input type="email" matInput [(ngModel)]="model.emailConfirm" name="emailConfirm" #emailConfirm="ngModel" | ||
required equalTo="email"> <!-- Notice how in this example we pass the name of email field --> | ||
<mat-error *ngIf="emailConfirm.hasError('required')">This field is required</mat-error> | ||
<mat-error *ngIf="emailConfirm.hasError('notEqualTo')">This field should be equal to Email field</mat-error> | ||
</mat-form-field> | ||
</div> | ||
<h3>Passing Field Reference</h3> | ||
<div class="row"> | ||
<mat-form-field class="col-6"> | ||
<mat-label>Password:</mat-label> | ||
<input type="password" matInput [(ngModel)]="model.password" name="password" #password="ngModel" required> | ||
<mat-error *ngIf="password.hasError('required')">This field is required</mat-error> | ||
</mat-form-field> | ||
<mat-form-field class="col-6"> | ||
<mat-label>Confirm Password:</mat-label> | ||
<input type="password" matInput [(ngModel)]="model.passwordConfirm" name="passwordConfirm" #passwordConfirm="ngModel" | ||
required [equalTo]="password"> <!-- Notice how in this example we pass the reference of password field --> | ||
<mat-error *ngIf="passwordConfirm.hasError('required')">This field is required</mat-error> | ||
<mat-error *ngIf="passwordConfirm.hasError('notEqualTo')">This field should be equal to Password field</mat-error> | ||
</mat-form-field> | ||
</div> | ||
</form> |
16 changes: 16 additions & 0 deletions
16
examples/src/app/equal-to-validator/form/equal-to/form-equal-to.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-equal-to', | ||
templateUrl: './form-equal-to.component.html' | ||
}) | ||
export class FormEqualToComponent implements OnInit { | ||
|
||
model = {}; | ||
|
||
constructor() { } | ||
|
||
ngOnInit() { | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...s/src/app/equal-to-validator/reactive-form/equal-to/reactive-form-equal-to.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<form [formGroup]="form"> | ||
<h3>Passing Field Name:</h3> | ||
<div class="row"> | ||
<mat-form-field class="col-6"> | ||
<mat-label>Email:</mat-label> | ||
<input type="email" matInput formControlName="email" required> | ||
<mat-error *ngIf="form.get('email').hasError('required')">This field is required</mat-error> | ||
</mat-form-field> | ||
<mat-form-field class="col-6"> | ||
<mat-label>Confirm Email:</mat-label> | ||
<input type="email" matInput formControlName="emailConfirm" required> | ||
<mat-error *ngIf="form.get('emailConfirm').hasError('required')">This field is required</mat-error> | ||
<mat-error *ngIf="form.get('emailConfirm').hasError('notEqualTo')">This field should be equal to Email field</mat-error> | ||
</mat-form-field> | ||
</div> | ||
<h3>Passing Field Reference</h3> | ||
<div class="row"> | ||
<mat-form-field class="col-6"> | ||
<mat-label>Password:</mat-label> | ||
<input type="password" matInput formControlName="password" required> | ||
<mat-error *ngIf="form.get('password').hasError('required')">This field is required</mat-error> | ||
</mat-form-field> | ||
<mat-form-field class="col-6"> | ||
<mat-label>Confirm Password:</mat-label> | ||
<input type="password" matInput formControlName="passwordConfirm" required> | ||
<mat-error *ngIf="form.get('passwordConfirm').hasError('required')">This field is required</mat-error> | ||
<mat-error *ngIf="form.get('passwordConfirm').hasError('notEqualTo')">This field should be equal to Password field</mat-error> | ||
</mat-form-field> | ||
</div> | ||
<div> | ||
<button mat-button [disabled]="form.invalid">Submit</button> | ||
</div> | ||
</form> |
25 changes: 25 additions & 0 deletions
25
...les/src/app/equal-to-validator/reactive-form/equal-to/reactive-form-equal-to.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; | ||
import { EmailValidators, EqualToValidator } from 'ngx-validators'; | ||
import { Component, OnInit } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-reactive-equal-to', | ||
templateUrl: './reactive-form-equal-to.component.html' | ||
}) | ||
export class ReactiveFormEqualToComponent implements OnInit { | ||
|
||
form: FormGroup; | ||
constructor(protected _fb: FormBuilder) { } | ||
|
||
ngOnInit() { | ||
this.form = this._fb.group({ | ||
email: [''], | ||
emailConfirm: [''], | ||
password: [''], | ||
passwordConfirm: [''] | ||
}, {validator: [ | ||
EqualToValidator.equalTo('email', 'emailConfirm'), | ||
EqualToValidator.equalTo('password', 'passwordConfirm') | ||
]}); | ||
} | ||
} |
Oops, something went wrong.