【问题标题】:minLength and maxLength validators not working in Angular7 Reactive FormminLength 和 maxLength 验证器在 Angular7 反应形式中不起作用
【发布时间】:2019-03-25 20:02:54
【问题描述】:

我正在尝试使用反应式表单在 Angular7 中使用 Validators.minLength 和 Validators.maxLength 但收到以下错误:

ERROR 错误:预期的验证器返回 Promise 或 Observable。

这是我的打字稿代码:

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)]

 })
}

这是我的模板 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>

你能看出是什么问题吗?

【问题讨论】:

  • 相当肯定它应该是:['', [Validators.required, Validators.minLength(2), Validators.maxLength(5)]] 作为一个数组
  • 需要注意的是minLength的错误码是minlength小写

标签: angular angular-reactive-forms angular-validation


【解决方案1】:

当你应用多个验证器时,它应该是一个数组:

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

 })
}

【讨论】:

  • 或者你可以使用interestRate: ['', Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(5)])]
  • @Rich 错误背后的原因是,FormControl 构造函数的第三个可选参数接受异步验证器,即 asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null ,因此它假定 Validators.minLength 是不正确的异步验证器。因此它抛出错误
猜你喜欢
  • 2013-08-19
  • 2022-10-08
  • 1970-01-01
  • 2022-11-05
  • 2013-05-30
  • 2016-10-30
  • 2019-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多