As I have previously asked in a question
I want to create a nested form where the parent is agnostic to the kid's formControlNames
So lets say we have a component componentA.component.ts
@Component({
selector: 'common-a',
template: `
<div [formGroup]="parentForm">
<div class="form-group">
<label>Common A[1]</label>
<div >
<input type="text" formControlName="valueA1">
<small>Description 1</small>
</div>
<div class="form-group">
<label>Common A[2]</label>
<div >
<input type="text" formControlName="valueA2">
<small>Description 2</small>
</div>
</div>
`
})
export class ComponentA implements OnInit{
@Input() parentForm: FormGroup;
constructor(private _fb: FormBuilder) {
}
ngOnInit() {
this.parentForm.addControl("valueA1", new FormControl('', Validators.required));
this.parentForm.addControl("valueA2", new FormControl('', Validators.required));
}
}
And the main component.
@Component({
selector: 'main',
template: `
<form [formGroup]="myForm" (ngSubmit)="onSubmit(myForm.value)">
<div>
<div *ngFor="let c of myForm.controls.componentA.controls; let i=index" class="form-group">
<common-a [parentForm]="c"></common-a>
</div>
<div>
<button type="submit" [disabled]="myForm.invalid">Register!</button>
<a class="button" (click)="add_component()">Add New</a>
<a class="button" (click)="delete_component()">Delete</a>
</div>
</div>
<pre>form value: <br>{{myForm.value | json}}</pre>
</form>
`
})
export class MainComponent implements OnInit{
@Input('group') public myForm: FormGroup;
add_component() {
const control = <FormArray>this.myForm.controls['componentA'];
control.push(this._fb.group({}));
}
delete_component() {
const control = <FormArray>this.myForm.controls['componentA'];
control.removeAt(this.myForm.length-1);
}
constructor(private _fb: FormBuilder) {
}
ngOnInit() {
this.myForm = this._fb.group({
componentA : this._fb.array([this._fb.group({})])
});
}
onSubmit(formValue) {
console.log(formValue);
}
}
My question now is how can I use patchValue
to fetch data from a server endpoint and populate the values of the form array, creating new FormGroups
on demand, always agnostic to the parent.
- One way I can see this can be done is that the parent generates new groups.
The problem with this approach, apart from the stinky architecture, is that I override the patchValue method with my own implementation and push these new groups on demand, since inside the function body I know how many there are, but the kid's OnInit will not be invoked within the function, leaving the values empty.
var self = this;
this.formArray.patchValue = (value: {[key: string]: any}, {onlySelf, emitEvent}: {onlySelf?: boolean, emitEvent?: boolean} = {}) =>{
for(var i = 0; i < Object.keys(value).length; i++ ) {
self.add_new();
}
Object.keys(value).forEach(name => {
if (self.formArray.controls[name]) {
self.formArray.controls[name].patchValue(value[name], {onlySelf: true, emitEvent});
}
});
self.formArray.updateValueAndValidity({onlySelf, emitEvent});
}
- The other option would be for the kid to provide a static function to expose it's internal composition.
And again I think this is bad design.
static generate() {
return new FormGroup({
valueA1: new FormControl('', Validators.required),
valueA2: new FormControl('', Validators.required)
});
}
Do you have any clean solution for this kind of problem ?
Also here's a working plunkr based on my previous question