我已经创建了一个验证器来检查两个formControls是否不同,但是我不知道如何检查formArray或formGroup的所有formControls是否相同。
我的代码:
export function MustDiffer(controlName: string, differControlName: string) {
return (formGroup: FormGroup) => {
const control = formGroup.controls[controlName];
const differControl = formGroup.controls[differControlName];
if (differControl.errors && !differControl.errors.mustDiffer) {
// return if another validator has already found an error on the differControl
return;
}
// return if the inputField has no input yet
if (control.value === "") {
return;
}
// set error on differControl if validation fails
if (control.value === differControl.value) {
differControl.setErrors({ mustDiffer: true }); //der Wert der wiedergegeben wird muss im Validator stehen also mustMatch, also eher im ngIf
} else {
differControl.setErrors(null);
}
}
}
另一个问题是我动态创建了formArray,但我不知道之前会有多少个FormControl。可以有1个,但也可以有13个。
他们被这样添加:
this.selectionArray.push(this.builder.control(null));
get selectionArray() {
return this.formGroup.get('selectionArray') as FormArray;
}