4

I am trying to use Validators.minLength and Validators.maxLength in Angular7 using a Reactive Form but get the following error:

ERROR Error: Expected validator to return Promise or Observable.

This is the typescript code I have:

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, FormBuilder, Validators } from 
  '@angular/forms';

@Component({
    selector: 'app-input-values',
    templateUrl: './input-values.component.html',
    styleUrls: ['./input-values.component.css']
})

export class InputValuesComponent implements OnInit {
   inputValuesForm: FormGroup;

constructor(private fb: FormBuilder) { }

ngOnInit() {
    this.inputValuesForm = this.fb.group({
    interestRate: ['', Validators.required, Validators.minLength(2), 
       Validators.maxLength(5)]

 })
}

And this is my template html:

<form [formGroup]="inputValuesForm" (ngSubmit)="onSubmit()" class="form-horizontal">
  <div class="panel panel-primary">
    <div class="panel-heading">
       <h3 class="panel-title">Input Data</h3>
    </div>

    <div class="panel-body">
      <div class="form-group" [ngClass]="{'has-error': inputValuesForm.get('interestRate').errors &&
     (inputValuesForm.get('interestRate').touched || inputValuesForm.get('interestRate').dirty)}">
        <label class="col-sm-4 control-label" for="interestRate">Interest Rate (omit percent sign):</label>
        <div class="col-sm-2">
          <input id="interestRate" formControlName="interestRate" type="text" class="form-control">
          <span class="help-block" *ngIf="inputValuesForm.get('interestRate').errors &&
        (inputValuesForm.get('interestRate').touched || inputValuesForm.get('interestRate').dirty)">
           <span *ngIf=" inputValuesForm.get('interestRate').errors.required">
             Interest Rate is required
           </span>
           <span
             *ngIf="inputValuesForm.get('interestRate').errors.minlength || inputValuesForm.get('interestRate').errors.maxlength">
              Interest Rate must be greater than 1 character and less than 6 characters
           </span>
         </span>
    </div>
  </div>

    </div>
  </div>
</form>

Can you see what the problem is?

2
  • Pretty sure it should be: ['', [Validators.required, Validators.minLength(2), Validators.maxLength(5)]] as an array Commented Mar 25, 2019 at 20:08
  • 1
    Should be noted that the error code for minLength is minlength lowercase Commented Oct 13, 2021 at 4:25

1 Answer 1

17

When you are applying multiple validators, It should be an array:

ngOnInit() {
    this.inputValuesForm = this.fb.group({
    interestRate: ['', [Validators.required, Validators.minLength(2), 
       Validators.maxLength(5)]]

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

2 Comments

or you can use interestRate: ['', Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(5)])]
@Rich the reason behind the error is , the third optional parameter of the FormControl constructor accepts an async Validator i.e asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null , so it assumes Validators.minLength to be an async validator which is not correct. Hence it throws the error

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.