在TS
这个函数首先在 ngOnInit 中调用。
initializeForm() {
this.rangeMappingForm = this.formBuilder.group({
mappingsList: this.formBuilder.array([], Validators.required)
});
}
在那之后,
public addFormSection(item?: any): void {
const formArray = this.rangeMappingForm.get('mappingsList') as FormArray;
formArray.push((this.initMappingRangeSection(item)));
}
private initMappingRangeSection(item?: any) {
return this.formBuilder.group({
fhFundDepartmentRangeMappingKey: [item?.fhFundDepartmentRangeMappingKey || this.getCustomId()],
fundStartRange: [(item?.fundStartRange || null), [Validators.required, Validators.minLength(4)]],
fundEndRange: [(item?.fundEndRange || null), [Validators.required, Validators.minLength(4)]],
departmentStartRange: [(item?.departmentStartRange || null), [Validators.required, Validators.minLength(4)]],
departmentEndRange: [(item?.departmentEndRange || null), [Validators.required, Validators.minLength(4)]]
});
}
我正确地得到了必需的错误,但是 minLength(html 中的最小长度)不起作用。
html代码如下,
<form [formGroup]="rangeMappingForm" id="gw-range-mapping-form" *ngIf="rangeMappingFormArrayRawValues?.length">
<ng-container formArrayName="mappingsList"
*ngFor="let rangeMappingControl of rangeMappingFormArrayRawValues; let i = index; trackBy:trackByFn">
<div [formGroupName]="i">
<article>
<div class="col-md-4">
<input type="number" formControlName="fundStartRange"
[ngClass]="{'is-invalid': rangeMappingFormArray.controls[i].get('fundStartRange')?.invalid && (isFormChanged || rangeMappingFormArray.controls[i].get('fundStartRange')?.touched || rangeMappingFormArray.controls[i].get('fundStartRange')?.dirty) }"
class="form-control gw-form-control" placeholder="Enter Range">