I have a form which contain a list of checkboxes and if one of them was checked an input form display.
This is my component.ts :
coveragestypes : Array<ItemPolicyModel>= [{id:'1', name :'type 1'},{id : '2',name :'type 2'},{id : '3',name :'type 3'},{id:'4',name:'type 4'}]
policyForm = new FormGroup({
coverages :new FormArray([]),
coveragesValue:new FormArray([]),
})
ngOnInit() {
this.names = this.coveragestypes.map(x => x.name)
this.addCheckboxes();
}
addCheckboxes() {
this.names.forEach(() => this.coverageFormArray.push(new FormControl()));
this.names.forEach(() => this.coveragesValueFormArray.push(new FormControl('', Validators.pattern(/^-?(0|[1-9]\d*)?$/))));
}
get coverageFormArray() {
return this.policyForm.controls.coverages as FormArray;
}
get coveragesValueFormArray() {
return this.policyForm.controls.coveragesValue as FormArray;
}
And this is my html :
<div class="checkbox">
<label formArrayName="coverages"
*ngFor="let coverage of coverageFormArray.controls;let i = index; ">
<input type="checkbox" kendoCheckBox [formControlName]="i" />
{{names[i]}}
<ng-container *ngIf="coverage.value">
<input type="text" [formControl]="policyForm.get('coveragesValue.'+i)">
</ng-container>
</label>
</div>
It is saved like that when I console the policyForm value : coverages: (4) [true, true, false, false] coveragesValue: (4) ["123", "555", "", ""]
or I want it to be saved in one form array like that :
coverages : [{id : '1', name : 'type1', value : '123'},{id : '2', name : 'type2', value : '555'}]
Can anyone help me please !