Custom Validator in Angular4 with dynamic parameter

93 Views Asked by At

In the form password field is there which has a specific pattern(small alphabet, capital alphabet, special, length)

components.ts

Initialize a boolean array for keeping track of pattern which is updated onkeyup:

passwd_pattern_ele: boolean[] = new Array(4);

Formcontrol for password field:

nPassword: ['', [Validators.required, Validators.minLength(8), password_pattern(this.passwd_pattern_ele).bind(this)]]

Custom validator:

This function takes passwd_pattern_ele array as parameter which is updated dynamically

function password_pattern(pass_pat_ele: boolean[]){
  return (control: AbstractControl): { [key: string]: any } | null =>{
    for (let i = 0; i < pass_pat_ele.length; i++) {
      var element = pass_pat_ele[i];
      if(!element){
        return { 'pass_pattern' : pass_pat_ele };
      }
    }
    return null;
  };
}

passwd_pattern_ele which is passed as parameter is not updating in side the function and is empty as when initialized. passwd_pattern_ele is updating outside.

How to get the updated data in passwd_pattern_ele array inside password_pattern()?

0

There are 0 best solutions below