minLength 和 maxLength 验证器在 Angular7 反应形式中不起作用
minLength and maxLength validators not working in Angular7 Reactive Form
我试图在 Angular7 中使用响应式表单使用 Validators.minLength 和 Validators.maxLength,但出现以下错误:
ERROR Error: Expected validator to return Promise or 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>
你能看出是什么问题吗?
当你应用多个验证器时,它应该是一个数组:
ngOnInit() {
this.inputValuesForm = this.fb.group({
interestRate: ['', [Validators.required, Validators.minLength(2),
Validators.maxLength(5)]]
})
}
我试图在 Angular7 中使用响应式表单使用 Validators.minLength 和 Validators.maxLength,但出现以下错误:
ERROR Error: Expected validator to return Promise or 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>
你能看出是什么问题吗?
当你应用多个验证器时,它应该是一个数组:
ngOnInit() {
this.inputValuesForm = this.fb.group({
interestRate: ['', [Validators.required, Validators.minLength(2),
Validators.maxLength(5)]]
})
}