How to create form with nesting fields, i know about formArray
in angular2 RC but am bit confused how to use it correctly ?
let suppose i have an form like this
// Main Form with formArray named as `global_modifier`
this.myForm = this._fb.group({
.......
name: ['', []],
global_modifier: this._fb.array([
this.initGlobalModifiers()
])
....
});
removeModifier(i: number) {
const control = <FormArray>this.myForm.controls['global_modifier'];
control.removeAt(i);
}
addModifier() {
const control = <FormArray>this.myForm.controls['global_modifier'];
control.push(this.initGlobalModifiers());
}
/*global_modifier function having nested fields named `items` .....*/
initGlobalModifiers() {
return this._fb.group({
.....
modifier_title: ['', []],
items: this._fb.array([
this.initItems()
])
.........
});
}
removeItem(i: number) {
const control = <FormArray>this.myForm.controls['items'];
control.removeAt(i);
}
addItem() {
const control = <FormArray>this.myForm.controls['items'];
control.push(this.initItems());
}
// items intilization
initItems() {
return this._fb.group({
item_title: ['', []],
item_price: ['', []]
});
}
now i am confused how to use this form in the html ??
i am trying this but did't work as expected..
<form [formGroup]="myForm" novalidate>
<input type="text" placeholder="name" formControlName="name" maxlength="50">
<div formArrayName="global_modifier" *ngFor="let cont of myForm.controls.global_modifier.controls; let i=index, let fst=first">
<div [formGroupName]="i">
<input type="text" placeholder="modifier_title" formControlName="modifier_title" maxlength="50">
<button *ngIf="fst" [ngClass]="{'inputAddButton ':fst}" (click)="addModifier(i)" type="button">
<i class="fa fa-plus fa-white" aria-hidden="true"></i>
</button>
<button *ngIf="!fst" [ngClass]="{'inputDeleteButton ':!fst}" (click)="removeModifier(i)">
<i class="fa fa-trash-o fa-white" aria-hidden="true"></i>
</button>
<!--block For form mlutiple entrys-------------------->
<div formArrayName="items">
<div *ngFor="let items of cont.items; let item_index=index, let fst=first">
<div [formGroupName]="i">
<div style="margin-bottom:10px">
............... NOTHING dISPLAY HERE ???
</div>
</div>
</div>
</div>
<!--block For form mlutiple entrys---=------------>
<br>
</div>
</div>
</form>
whats the error in my code ? or anyone having working example of nested forms in angular2 ?
Nested Form with nested ARRAY
Plunker : https://plnkr.co/edit/sUjE1ULYhfDHLNBw2sRv?p=info Scotch.IO : https://scotch.io/tutorials/how-to-build-nested-model-driven-forms-in-angular-2
I think your error is in
you should push to your Form/FormGroup object not to control. See the PLUNKER to see the working example.
correct adding method :
Nested Form with nested OBJECTS
...
Check this example that worked for me until rc4 (didn't check in newer versions):
form markup
html markup
Remarks
this.myForm = this.formBuilder.group
creates a form object with user's configuration and assigns it to this.myForm variable.
'loginCredentials': this.formBuilder.group
method creates a group of controls which consist of a formControlName eg.
login
and value['', Validators.required],
where the first parameter is the initial value of the form input and the secons is a validator or an array of validators as in'email': ['', [Validators.required, customValidator]],
.Creates an array of groups where the index of the group is formGroupName in the array and is accessed like :
this sample method adds new formGroup to the array. Currently accessing requires specifing the type of control we want to access, in this example this type is :
<FormArray>
same rules as above apply to removing a specific form control from the array