Angular 7 反应式表单生成器,自定义交叉验证器不工作
Angular 7 Reactive Form Builder, Custom Cross Validator Not Working
这是我的 HTML:
<mat-form-field>
<input matInput type="number" min="0" max="100" step="1" placeholder="Allowed Attendance (%)" required [formControl]="allowed" name="allowed_attendance">
<mat-error *ngIf="allowed.invalid">Value must be 0-100</mat-error>
</mat-form-field>
<mat-form-field>
<input matInput type="number" min="0" max="100" step="1" placeholder="Fined Attendance (%)" required [formControl]="fined" name="fined_attendance">
<mat-error *ngIf="fined.invalid">This value must be less than allowed attendance</mat-error>
</mat-form-field>
<mat-form-field>
<input matInput required [formControl]="ldo_form_fill_up" placeholder="Applicable Until" [matDatepicker]="picker" name="ldo_form_fill_up">
<mat-datepicker-toggle #toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
<mat-error *ngIf="ldo_form_fill_up.invalid">A valid date is required</mat-error>
</mat-form-field>
<mat-form-field *ngIf="ldo_form_fill_up.valid">
<input matInput required [formControl]="ldo_payment" placeholder="Payable Until" [matDatepicker]="picker2" name="ldo_payment">
<mat-datepicker-toggle #toggle2 matSuffix [for]="picker2"></mat-datepicker-toggle>
<mat-datepicker #picker2></mat-datepicker>
<mat-error *ngIf="ldo_payment.invalid">This date must be greater than the above</mat-error>
</mat-form-field>
这里是 getter 表单控件:
get ldo_form_fill_up() {
return this.form.get('ldo_form_fill_up');
}
get ldo_payment() {
return this.form.get('ldo_payment');
}
get allowed() {
return this.form.get('allowed_attendance');
}
get fined() {
return this.form.get('fined_attendance');
}
这是表单生成器:
this.form = fb.group({
allowed_attendance: fb.control(70, [Validators.required, Validators.min(0), Validators.max(100)]),
fined_attendance: fb.control(60, [Validators.required, Validators.min(0), Validators.max(100)]),
ldo_form_fill_up: fb.control('', Validators.required),
ldo_payment: fb.control('', Validators.required),
}, {
validators: customRangeValidator
});
这是自定义验证器:
export function customRangeValidator(group: FormGroup) {
const date1 = group.controls.ldo_form_fill_up;
const date2 = group.controls.ldo_payment;
const attd1 = group.controls.allowed_attendance;
const attd2 = group.controls.fined_attendance;
if (date1.value >= date2.value) {
date2.setErrors({
customRangeValidator: true
});
}
if (attd2.value >= attd1.value) {
attd2.setErrors({
customRangeValidator: true
});
}
return null;
}
我需要的是:fined_attendance < allowed_attendance
和ldo_payment > ldo_form_fill_up
发生了什么:
- 在
allowed_attendance
中输入70
- 在
fined_attendance
中输入60
fined_attendance
没有错误(没问题)。
- 将
allowed_attendance
更改为 50
。
fined_attendance
显示错误(也正常)。
- 再次将
allowed_attendance
更改为 70
。
- 仍然显示错误。 (不行).
- 但是在
fined_attendance
中清除并重新输入 60
,错误消失了!
所以,我遇到了 step 7
中显示的问题。我做错了什么?
我认为您只关心设置错误而不关心清除错误。
在else条件下你也应该清除它们。方法如下:
import { Component } from '@angular/core';
import { FormBuilder, Validators, FormGroup } from '@angular/forms';
/** @title Simple form field */
@Component({
selector: 'form-field-overview-example',
templateUrl: 'form-field-overview-example.html',
styleUrls: ['form-field-overview-example.css'],
})
export class FormFieldOverviewExample {
form: FormGroup;
constructor(private fb: FormBuilder) { }
ngOnInit() {
this.form = this.fb.group({
allowed_attendance: this.fb.control(70, [Validators.required, Validators.min(0), Validators.max(100)]),
fined_attendance: this.fb.control(60, [Validators.required, Validators.min(0), Validators.max(100)]),
ldo_form_fill_up: this.fb.control('', Validators.required),
ldo_payment: this.fb.control('', Validators.required),
}, { validators: customRangeValidator });
}
submit() {
console.log(this.form.value);
}
get ldo_form_fill_up() {
return this.form.get('ldo_form_fill_up');
}
get ldo_payment() {
return this.form.get('ldo_payment');
}
get allowed() {
return this.form.get('allowed_attendance');
}
get fined() {
return this.form.get('fined_attendance');
}
}
export function customRangeValidator(group: FormGroup) {
console.log('Called');
const { ldo_form_fill_up, ldo_payment, allowed_attendance, fined_attendance } = group.controls;
if (fined_attendance.value >= allowed_attendance.value) {
fined_attendance.setErrors({ customRangeValidator: true });
} else {
fined_attendance.setErrors(null);
}
if (ldo_form_fill_up.value >= ldo_payment.value) {
ldo_payment.setErrors({ customRangeValidator: true });
} else {
ldo_payment.setErrors(null);
}
return null;
}
此外,在您的模板中,为什么不直接使用 formGroupName
而不是使用 [formGroup]
。像这样:
<form [formGroup]="form">
<mat-form-field>
<input matInput type="number" min="0" max="100" step="1" placeholder="Allowed Attendance (%)" required formControlName="allowed_attendance" name="allowed_attendance">
<mat-error *ngIf="allowed.invalid">Value must be 0-100</mat-error>
</mat-form-field>
<mat-form-field>
<input matInput type="number" min="0" max="100" step="1" placeholder="Fined Attendance (%)" required formControlName="fined_attendance" name="fined_attendance">
<mat-error *ngIf="fined.invalid">This value must be less than allowed attendance</mat-error>
</mat-form-field>
<mat-form-field>
<input matInput required formControlName="ldo_form_fill_up" placeholder="Applicable Until" [matDatepicker]="picker" name="ldo_form_fill_up">
<mat-datepicker-toggle #toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
<mat-error *ngIf="ldo_form_fill_up.invalid">A valid date is required</mat-error>
</mat-form-field>
<mat-form-field *ngIf="ldo_form_fill_up.valid">
<input matInput required formControlName="ldo_payment" placeholder="Payable Until" [matDatepicker]="picker2" name="ldo_payment">
<mat-datepicker-toggle #toggle2 matSuffix [for]="picker2"></mat-datepicker-toggle>
<mat-datepicker #picker2></mat-datepicker>
<mat-error *ngIf="ldo_payment.invalid">This date must be greater than the above</mat-error>
</mat-form-field>
<br>
<button
[disabled]="form.invalid"
mat-raised-button
color="primary"
(click)="submit()">
Primary
</button>
</form>
Here's a Working Sample StackBlitz for your ref.
这是我的 HTML:
<mat-form-field>
<input matInput type="number" min="0" max="100" step="1" placeholder="Allowed Attendance (%)" required [formControl]="allowed" name="allowed_attendance">
<mat-error *ngIf="allowed.invalid">Value must be 0-100</mat-error>
</mat-form-field>
<mat-form-field>
<input matInput type="number" min="0" max="100" step="1" placeholder="Fined Attendance (%)" required [formControl]="fined" name="fined_attendance">
<mat-error *ngIf="fined.invalid">This value must be less than allowed attendance</mat-error>
</mat-form-field>
<mat-form-field>
<input matInput required [formControl]="ldo_form_fill_up" placeholder="Applicable Until" [matDatepicker]="picker" name="ldo_form_fill_up">
<mat-datepicker-toggle #toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
<mat-error *ngIf="ldo_form_fill_up.invalid">A valid date is required</mat-error>
</mat-form-field>
<mat-form-field *ngIf="ldo_form_fill_up.valid">
<input matInput required [formControl]="ldo_payment" placeholder="Payable Until" [matDatepicker]="picker2" name="ldo_payment">
<mat-datepicker-toggle #toggle2 matSuffix [for]="picker2"></mat-datepicker-toggle>
<mat-datepicker #picker2></mat-datepicker>
<mat-error *ngIf="ldo_payment.invalid">This date must be greater than the above</mat-error>
</mat-form-field>
这里是 getter 表单控件:
get ldo_form_fill_up() {
return this.form.get('ldo_form_fill_up');
}
get ldo_payment() {
return this.form.get('ldo_payment');
}
get allowed() {
return this.form.get('allowed_attendance');
}
get fined() {
return this.form.get('fined_attendance');
}
这是表单生成器:
this.form = fb.group({
allowed_attendance: fb.control(70, [Validators.required, Validators.min(0), Validators.max(100)]),
fined_attendance: fb.control(60, [Validators.required, Validators.min(0), Validators.max(100)]),
ldo_form_fill_up: fb.control('', Validators.required),
ldo_payment: fb.control('', Validators.required),
}, {
validators: customRangeValidator
});
这是自定义验证器:
export function customRangeValidator(group: FormGroup) {
const date1 = group.controls.ldo_form_fill_up;
const date2 = group.controls.ldo_payment;
const attd1 = group.controls.allowed_attendance;
const attd2 = group.controls.fined_attendance;
if (date1.value >= date2.value) {
date2.setErrors({
customRangeValidator: true
});
}
if (attd2.value >= attd1.value) {
attd2.setErrors({
customRangeValidator: true
});
}
return null;
}
我需要的是:fined_attendance < allowed_attendance
和ldo_payment > ldo_form_fill_up
发生了什么:
- 在
allowed_attendance
中输入 - 在
fined_attendance
中输入 fined_attendance
没有错误(没问题)。- 将
allowed_attendance
更改为50
。 fined_attendance
显示错误(也正常)。- 再次将
allowed_attendance
更改为70
。 - 仍然显示错误。 (不行).
- 但是在
fined_attendance
中清除并重新输入60
,错误消失了!
70
60
所以,我遇到了 step 7
中显示的问题。我做错了什么?
我认为您只关心设置错误而不关心清除错误。
在else条件下你也应该清除它们。方法如下:
import { Component } from '@angular/core';
import { FormBuilder, Validators, FormGroup } from '@angular/forms';
/** @title Simple form field */
@Component({
selector: 'form-field-overview-example',
templateUrl: 'form-field-overview-example.html',
styleUrls: ['form-field-overview-example.css'],
})
export class FormFieldOverviewExample {
form: FormGroup;
constructor(private fb: FormBuilder) { }
ngOnInit() {
this.form = this.fb.group({
allowed_attendance: this.fb.control(70, [Validators.required, Validators.min(0), Validators.max(100)]),
fined_attendance: this.fb.control(60, [Validators.required, Validators.min(0), Validators.max(100)]),
ldo_form_fill_up: this.fb.control('', Validators.required),
ldo_payment: this.fb.control('', Validators.required),
}, { validators: customRangeValidator });
}
submit() {
console.log(this.form.value);
}
get ldo_form_fill_up() {
return this.form.get('ldo_form_fill_up');
}
get ldo_payment() {
return this.form.get('ldo_payment');
}
get allowed() {
return this.form.get('allowed_attendance');
}
get fined() {
return this.form.get('fined_attendance');
}
}
export function customRangeValidator(group: FormGroup) {
console.log('Called');
const { ldo_form_fill_up, ldo_payment, allowed_attendance, fined_attendance } = group.controls;
if (fined_attendance.value >= allowed_attendance.value) {
fined_attendance.setErrors({ customRangeValidator: true });
} else {
fined_attendance.setErrors(null);
}
if (ldo_form_fill_up.value >= ldo_payment.value) {
ldo_payment.setErrors({ customRangeValidator: true });
} else {
ldo_payment.setErrors(null);
}
return null;
}
此外,在您的模板中,为什么不直接使用 formGroupName
而不是使用 [formGroup]
。像这样:
<form [formGroup]="form">
<mat-form-field>
<input matInput type="number" min="0" max="100" step="1" placeholder="Allowed Attendance (%)" required formControlName="allowed_attendance" name="allowed_attendance">
<mat-error *ngIf="allowed.invalid">Value must be 0-100</mat-error>
</mat-form-field>
<mat-form-field>
<input matInput type="number" min="0" max="100" step="1" placeholder="Fined Attendance (%)" required formControlName="fined_attendance" name="fined_attendance">
<mat-error *ngIf="fined.invalid">This value must be less than allowed attendance</mat-error>
</mat-form-field>
<mat-form-field>
<input matInput required formControlName="ldo_form_fill_up" placeholder="Applicable Until" [matDatepicker]="picker" name="ldo_form_fill_up">
<mat-datepicker-toggle #toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
<mat-error *ngIf="ldo_form_fill_up.invalid">A valid date is required</mat-error>
</mat-form-field>
<mat-form-field *ngIf="ldo_form_fill_up.valid">
<input matInput required formControlName="ldo_payment" placeholder="Payable Until" [matDatepicker]="picker2" name="ldo_payment">
<mat-datepicker-toggle #toggle2 matSuffix [for]="picker2"></mat-datepicker-toggle>
<mat-datepicker #picker2></mat-datepicker>
<mat-error *ngIf="ldo_payment.invalid">This date must be greater than the above</mat-error>
</mat-form-field>
<br>
<button
[disabled]="form.invalid"
mat-raised-button
color="primary"
(click)="submit()">
Primary
</button>
</form>
Here's a Working Sample StackBlitz for your ref.