Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Reactive Forms - FormGroupName inside FormGroupName

I have a nested JSON response coming back from an API and is structured differently then how I need to display it on the template, for example:

@Component({
  selector: 'reactive-form-example',
  styles: ['./reactive-form-example.component.css'],
  template: `
    <form [formGroup]="form" (ngSubmit)="onSubmit()">
        <div formGroupName="first">
            <input type="text" placeholder="some id" formControlName="someId">
            <div formGroupName="second">
                <input type="text" placeholder="some text" formControlName="someText">
            </div>
        </div>
    </form>
  `
})
export class ReactiveFormExampleComponent {
    form = new FormGroup({
        first: new FormGroup({
            someId: new FormControl('587824')
        }),
        second: new FormGroup({
            someText: new FormControl('the sky is blue')
        })
    });
    onSubmit(value) {
        console.log('Submit', this.form.value);
    }
}

Question: Is it possible to have a formGroupName nested within another formGroupName, or is there a better way to achieve this same result using reactive forms?

like image 385
Jordan Davis Avatar asked May 13 '18 20:05

Jordan Davis


People also ask

What is formgroupname in angular 10?

Angular forms FormGroupName Directive Last Updated : 04 Jul, 2021 In this article, we are going to see what is FormGroupName in Angular 10 and how to use it. The FormGroupName is used to sync a nested FormGroup to a DOM element.

Can formgroupname be nested within another formgroup name?

Yes. formGroupName can be nested within another formGroupName. formGroupName and formControlName attributes work by finding a control with that particular name within the parent FormGroup. Note that your problem is caused because you're trying to find a FormGroup named second within the first FormGroup:

What are reactive forms in angular?

What we do with reactive forms instead, is keeping the data in the form model, until the user hits the submit button. Only then the data is copied over to the original model, replacing everything. This type of overwriting everything is called "immutable objects". To create this form-model, angular uses a class called FormGroup.

How to create a form-model in Angular 2?

To create this form-model, angular uses a class called FormGroup. To define our form-model, we create a new FormGroup. The constructor of this class then takes an object, that can contain sub-form-groups and FormControls.


1 Answers

Yes. formGroupName can be nested within another formGroupName.

formGroupName and formControlName attributes work by finding a control with that particular name within the parent FormGroup.

Note that your problem is caused because you're trying to find a FormGroup named second within the first FormGroup:

<form [formGroup]="form">
    <div formGroupName="first">
        <div formGroupName="second">
        </div>
    </div>
</form>

To make that work, you'd have to adapt your model as follows, where second becomes a child of first:

form = new FormGroup({
    first: new FormGroup({
        someId: new FormControl('587824'),
        second: new FormGroup({
            someText: new FormControl('the sky is blue')
        })
    }),        
});

The reason why the suggestion of emostafa works, is because you ask the form instance to get a direct child named second within the model. Which does exist in this case.

like image 79
David Walschots Avatar answered Sep 20 '22 08:09

David Walschots