1

I want to display a red border round the input field of a form and the message Password is invalid and when a user types in a password that doesn't match what the password has been set to.

I can see the red border indicating that there is an error when I type something different from the set password but the message is not displayed. I haven't been able to figure out what the problem is after spending several hours going through my code so kindly look at my code and point me to the bug

password.validators.ts

import { AbstractControl, ValidationErrors } from "@angular/forms";

export class PasswordValidators {
    static matchOldPassword(
       control: AbstractControl
       ): Promise<ValidationErrors | null> {
       return new Promise((resolve, reject) => {
           setTimeout(() => {
           if (control.value !== "1234") resolve({ matchOldPassword: true });
           else resolve(null);
           }, 2000);
       });
   }
}

password-change.component.ts

import { PasswordValidators } from "./password.validator";
import { FormGroup, FormControl, Validators } from "@angular/forms";
import { Component } from "@angular/core";

@Component({
  selector: "password-change",
  templateUrl: "./password-change.component.html",
  styleUrls: ["./password-change.component.css"]
})
export class PasswordChangeComponent {
  form = new FormGroup({
    oldPassword: new FormControl(
    "",
    Validators.required,
    PasswordValidators.matchOldPassword
    )
  });
}

password-change.component.html

<form [formGroup]="form">
<div class="form-group">
<label for="oldPassword" class="font-weight-bold">Old Password</label>
<input
  formControlName="oldPassword"
  type="password"
  id="oldPassword"
  class="form-control"
/>
<div
  *ngIf="oldPassword.touched && oldPassword.pristine"
  class="text-danger"
>
  <div *ngIf="oldPassword.errors.required">Old password is required.</div>
  <div *ngIf="oldPassword.errors.matchOldPassword">
    Old password is not valid.
  </div>
</div>
</div>
</form>
1
  • remove && oldPassword.pristine... from *ngIf. Commented Feb 7, 2019 at 19:33

1 Answer 1

1

Remove oldPassword.pristine from your wrapping div. Your if becomes falsy after the field has been modified, since it's no longer pristine, so the whole div won't show. Else, your code looks fine! So your wrapping div should just look like:

<div *ngIf="oldPassword.touched">

StackBlitz

Sign up to request clarification or add additional context in comments.

5 Comments

Removing pristine worked but threw some errors until I changed this bit which I seem kind of confused about <div *ngIf="oldPassword?.errors?.required">Old password is required.</div> <div *ngIf="oldPassword?.errors?.matchOldPassword"> A little explanation on why this worked perfectly. Thanks
Riiight. The ? is the safe navigation operator: angular.io/guide/… I think that explains it well. Very useful operator! Do ask if there is something you are still wondering :)
You can also like (what I prefer) this way to display error message: oldPassword.hasError('required')" etc
So in this case of custom validation, will this oldPassword.hasError('matchOldPassword') work?
Yes that worked and looks cleaner for me. Cool tip chief

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.