1

I have created the form group as below

import { setPwd } from './validators';

@Component({
....
export class pwdComp {
     ...

     this.userFormPassword = this.fb.group({
       'password': ['', [ setPwd ]]
     });

In my another ts file has the setPwd method

export function setPwd(c: FormControl) {
  const SINGLE_LETTER_NUMERAL = /^\=\?$/;
  return SINGLE_LETTER_NUMERAL.test(c.value) ? null : {
    pwd: {
      valid: false
    }
  };
}

The above script is working fine. But now my new scenario is I want to pass the pattern attribute to the setPwd method, So I tried as below

this.userFormPassword = this.fb.group({
    'password': ['', [ setPwd('**') ]]
});

and the setPwd method is

export function setPwd(c: FormControl, newPattern) {

But it is throwing the error. How to pass the extra value to the external function.

mkHun
  • 5,891
  • 8
  • 38
  • 85

1 Answers1

1

You should change your function like below

export function setPwd(pattern: RegExp): ValidatorFn {
  return (control: AbstractControl): {[key: string]: any} | null => {
    const forbidden = pattern.test(control.value);
    return forbidden ? {'pwd': {value: control.value}} : null;
  };
}

this.userFormPassword = this.fb.group({
    'password': ['', setPwd('**') ]
});
Nithya Rajan
  • 4,722
  • 19
  • 30