Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error when using my own validator (Angular, Reactive-Forms)

This is my piece of code, when I try to use an own validator, to make sure, the name does not contain numbers:

ngOnInit() {
// HERE
    this.bewerberForm = new FormGroup({
      'name': new FormControl(null, Validators.required, this.containsNumbers),
      'email': new FormControl(null, [Validators.required, Validators.email]),
    ...

  }

  containsNumbers(control: FormControl): Promise<any> | Observable<any> {
    const promise = new Promise<any>((resolve, reject) => {
      for (let i = 0; i < control.value.length; i++) {
        if (control.value.charAt(i).match('[0-9]')) {
          resolve({ 'nameIsForbidden': true });
          break;
        } else {
          resolve(null);
        }
      }
    });
    return promise;
  }

I get the following error, which I don’t understand, since I already did that in another project:

No overload matches this call.
  Overload 1 of 5, '(value: FormControlState<null> | null, opts: FormControlOptions, asyncValidator: AsyncValidatorFn | AsyncValidatorFn[]): FormControl<...>', gave the following error.
    Type '(control: AbstractControl<any, any>) => ValidationErrors | null' has no properties in common with type 'FormControlOptions'.
  Overload 2 of 5, '(value: FormControlState<null> | null, validatorOrOpts?: FormControlOptions | ValidatorFn | ValidatorFn[] | null | undefined, asyncValidator?: AsyncValidatorFn | ... 2 more ... | undefined): FormControl<...>', gave the following error.
    Argument of type '(control: FormControl<any>) => Promise<any> | Observable<any>' is not assignable to parameter of type 'AsyncValidatorFn | AsyncValidatorFn[] | null | undefined'.
      Type '(control: FormControl<any>) => Promise<any> | Observable<any>' is not assignable to type 'AsyncValidatorFn'.
        Types of parameters 'control' and 'control' are incompatible.
          Type 'AbstractControl<any, any>' is missing the following properties from type 'FormControl<any>': defaultValue, registerOnChange, registerOnDisabledChangets(2769)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

'name': new FormControl(null, Validators.required, this.containsNumbers),

should be

'name': new FormControl(null, [Validators.required, this.containsNumbers]),

Validators are passed in an array, just like you did in the email control.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading