Skip to content

Commit c5a1e7e

Browse files
committed
Cross field validation
1 parent 1a3a0f2 commit c5a1e7e

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

reactive-forms/src/app/app.component.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ <h2>Registration Form</h2>
2020

2121
<div class="form-group">
2222
<label>Confirm Password</label>
23-
<input type="password" formControlName="confirmPassword" class="form-control">
23+
<input type="password" [class.is-invalid]="registrationForm.errors?.misMatch" formControlName="confirmPassword" class="form-control">
24+
<small class="text-danger" *ngIf="registrationForm.errors?.misMatch">Passwords do not match</small>
2425
</div>
2526

2627
<div formGroupName="address">
@@ -47,5 +48,6 @@ <h2>Registration Form</h2>
4748

4849
</form>
4950

51+
{{registrationForm.status | json}}
5052
{{registrationForm.value | json}}
5153
</div>

reactive-forms/src/app/app.component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Component } from '@angular/core';
2-
import { FormGroup, FormControl } from '@angular/forms';
2+
import { FormGroup, FormControl, Validators } from '@angular/forms';
33
import { FormBuilder } from '@angular/forms';
4-
import { Validators } from '@angular/forms';
5-
import { forbiddenNameValidator } from './shared/user-name.validator';
4+
import { ForbiddenNameValidator } from './shared/user-name.validator';
5+
import { PasswordValidator } from './shared/password.validator';
66

77
@Component({
88
selector: 'app-root',
@@ -23,15 +23,15 @@ export class AppComponent {
2323
// });
2424

2525
registrationForm = this.fb.group({
26-
userName: ['', [Validators.required, Validators.minLength(3), forbiddenNameValidator(/password/)]],
26+
userName: ['', [Validators.required, Validators.minLength(3), ForbiddenNameValidator(/password/)]],
2727
password: [''],
2828
confirmPassword: [''],
2929
address: this.fb.group({
3030
city: [''],
3131
state: [''],
3232
postalCode: ['']
3333
})
34-
});
34+
}, {validator: PasswordValidator});
3535

3636
get userName() {
3737
return this.registrationForm.get('userName');
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { AbstractControl } from '@angular/forms';
2+
3+
export function PasswordValidator(control: AbstractControl): { [key: string]: boolean } | null {
4+
const password = control.get('password');
5+
const confirmPassword = control.get('confirmPassword');
6+
if (password.pristine || confirmPassword.pristine) {
7+
return null;
8+
}
9+
return password && confirmPassword && password.value !== confirmPassword.value ? { 'misMatch': true } : null;
10+
}

reactive-forms/src/app/shared/user-name.validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ValidatorFn, AbstractControl } from '@angular/forms';
22

3-
export function forbiddenNameValidator(forbiddenName: RegExp): ValidatorFn {
3+
export function ForbiddenNameValidator(forbiddenName: RegExp): ValidatorFn {
44
return (control: AbstractControl): { [key: string]: any } | null => {
55
const forbidden = forbiddenName.test(control.value);
66
return forbidden ? { 'forbiddenName': { value: control.value } } : null;

0 commit comments

Comments
 (0)