我有一个带有2个控件(port_start和port_end)的反应形式,它们有以下要求:
这是我到目前为止所尝试的:
[...]
this.formModel.addControl('port_start',
new FormControl(object.port_start ? object.port_start : 0,
[Validators.required, Validators.min(0), Validators.max(65535), this.minMaxValidator('port_start', 'port_end').bind(this)]));
this.formModel.addControl('port_end',
new FormControl(object.ort_end ? object.port_end : 0,
[Validators.required, Validators.min(0), Validators.max(65535), this.minMaxValidator('port_start', 'port_end').bind(this)]));
[...]
这是自定义验证器功能:
minMaxValidator = function(startControl : string, endControl : string): ValidatorFn {
return (control: FormControl): {[key: string]: any} => {
let valid = true;
let valStart = 0;
let valEnd = 0;
if(this.formModel.controls[startControl] && this.formModel.controls[endControl]) {
valStart = <number>this.formModel.controls[startControl].value;
valEnd = <number>this.formModel.controls[endControl].value;
}
valid = valEnd >= valStart;
return valid ? null : { minmax : true };
};
}
除了这个问题,这个工作正常:
据我所知,问题是我每次更改另一个值时都需要重新检查相关字段,但我不知道该怎么做。
有什么想法吗?谢谢,
答案 0 :(得分:6)
与@Tomasz说的不同,您可以将验证保持在控制级别。
秘密:收听表单上的更改,并在发生更改时,使用当前值设置验证程序。
Here is a stackblitz ,向您展示它的工作原理。
以下是我使用的代码:
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, ValidatorFn, AbstractControl } from '@angular/forms';
@Component({
selector: 'my-app',
template: `
<form [formGroup]="form">
<input type="number" formControlName="start">
<input type="number" formControlName="end">
</form>
<div *ngIf="form.get('start').hasError('lessThan')">Min should be less than max</div>
<div *ngIf="form.get('end').hasError('moreThan')">Max should be less than min</div>
`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
form: FormGroup;
constructor(private fb: FormBuilder) {
this.form = fb.group({
start: [0],
end: [65535]
});
this.form.valueChanges.subscribe(changes => {
this.form.get('start').setValidators(LessThanEnd(+this.form.value.end));
this.form.get('end').setValidators(MoreThanStart(+this.form.value.start));
});
}
}
export function LessThanEnd(end: number): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => {
return control.value > end ? { 'lessThan': true } : null;
};
}
export function MoreThanStart(end: number): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => {
return control.value < end ? { 'moreThan': true } : null;
};
}
答案 1 :(得分:5)
min
,max
和required
验证程序可以保留原样。如果要根据另一个控件的值验证一个控件,则需要将验证提升到父控件。
import { Component } from '@angular/core';
import { ValidatorFn, FormBuilder, FormGroup, Validators } from '@angular/forms';
const portStartEnd: ValidatorFn = (fg: FormGroup) => {
const start = fg.get('portStart').value;
const end = fg.get('portEnd').value;
return start && end && start < end ? null : { startEnd: true };
}
@Component({
selector: 'my-app',
template: `
<input [formControl]="form.get('portStart')" type="number" >
<input [formControl]="form.get('portEnd')" type="number" >
{{ form.valid }}
`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
form: FormGroup;
constructor(private fb: FormBuilder) {
this.form = this.fb.group({
portStart: [null, [Validators.required, Validators.min(0), Validators.max(65535)]],
portEnd: [null, [Validators.required, Validators.min(0), Validators.max(65535)]]
}, { validator: portStartEnd } );
}
}