When creating a form group within another form group, ensure that formGroup is passed a FormGroup instance

I have created a form for adding users, which includes fields to input their birthdate.

 this.userFG = this.formBuilder.group({
  name: [""],
  family: [""],
  birthDate: this.formBuilder.group({
    day: [""],
    month: [""],
    year: [""]
  })
});

When implementing it in the HTML file, I use the following:

    <form [formGroup]="userFG">
    <div>
        <label>Name : </label>
        <input formControlName="name">
  </div>
        <div>
            <label>lastName : </label>
            <input formControlName="family">
  </div>
            <div [formGroup]="birthDate">
                <div>
                    <label>day : </label>
                    <input formControlName="day">
  </div>
                    <div>
                        <label>month : </label>
                        <input formControlName="month">
  </div>
                        <div>
                            <label>year : </label>
                            <input formControlName="year">
  </div>
                        </div>
</form>

After running the project, an error message is displayed:

formGroup expects a FormGroup instance. Please pass one

You can see a demo of the issue here.

What could be causing this problem and how can it be resolved?

Answer №1

If you're dealing with a nested form group, make sure to utilize formGroupName for the inner form group:

<div formGroupName="birthDate">
   ... 
   <input formControlName="day">
   ... 
   <input formControlName="month">
   ...
   <input formControlName="year">
<div>

To see this in action, check out this Working Stackblitz

If you need more information on how to use formGroupName, refer to the official documentation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Combining the values of two input fields in Angular

Within my form, I have three input fields labeled Name, hours, and minutes. When I execute a POST operation, I combine the values of hours and minutes into a variable named duration before sending it to the api. The resulting JSON structure appears as fo ...

IE browser transparency effect on canvas

I have encountered an issue when drawing a shape on canvas using kineticJS and setting the fill to none. The code snippet I am using is below: var rect = new Kinetic.Path({ x: 0, y: 0, data: 'm 2.0012417,2.0057235 ...

Reorganize the data in a different manner (collections of items instead of individual items linked to groups)

Is there a way to create a function that converts data from `DevicesType` to `GroupsType` below? Instead of having a list of devices showing the groups they belong to, I need a list of groups with their respective devices. type DevicesType = { id: string ...

"Implementing a feature to dynamically load Blogspot post content using JSON upon user

$.ajax({ url: 'https://uniquewebsite.com/feeds/posts/default?start-index=1&max-results=2&alt=json-in-script', type: 'get', dataType: "jsonp", success: function(data) { var entry = data.feed.entry; ...

I am facing difficulties with the header in CSS as it is not resizing properly

I'm having trouble getting my mobile-first design to work properly because the header doesn't resize in mobile view. I'm satisfied with how it looks in full-screen, but I haven't been able to make any media queries work. You can downloa ...

Converting binary data to a datetime object with AngularJS

Need help converting a date format to the normal date format. For example, from 12-07-2017. Currently, my output looks like this: /Date(1508896907290) I am working with AngularJS. {{list.DateStamp}} Any suggestions on how to convert binary format to ...

Include a class in ul > li elements upon page load in Angular4

I attempted to add a class to each "li" element in an Angular4 page, but the class was not applied. Here is the relevant HTML code: <ul class="pagination"> <button class="previous" (click)="previous()">Previous</button> <button ...

React Application not reflecting recent modifications made to class

My current project involves creating a transparent navigation bar that changes its background and text color as the user scrolls. Utilizing TailwindCSS for styling in my React application, I have successfully implemented the functionality. // src/componen ...

Configuring Dialog Placement to the Top in Material-UI

I'm in the process of creating a popup dialog, but I'm encountering an issue where the dialog pops up in the center of the page. How can I position it at the very top of the page when the popup button is clicked? Below is the code snippet: < ...

Displaying Firebase data using Angularfire2 5.0 on an Ionic template

Hey everyone, I've been encountering a problem while trying to use angularfire2 v 5.0. I was comfortable using v 4.0 before, but now that I'm transitioning to v 5.0, I'm facing some issues. Does anyone know how I can display real-time data ...

How can I extract only certain keys from a large JavaScript object while keeping the code concise?

Simply put, I aim to streamline objects by discarding unnecessary keys. Imagine a scenario where a third party API sends back JSON data with numerous attributes that hold no importance to you. obj = { name: ..., id: ..., description: ..., blah: .. ...

When the disk space is insufficient, the createWriteStream function will not trigger an error event if the file is not completely written

One challenge I'm encountering involves using createWriteStream: Imagine I have a large 100mb file that I want to write to another file on the disk. The available space on the disk is only 50mb. Here's my code snippet: const fs = require(&a ...

Incorporate a different address using $location.path(path); and submit the ng-form

I have a form: <form name="myForm" ng-submit="save(myObject)"> ... <button type="submit" class="btn btn-primary" ng-click="changeLocation('/railway-connection')">Save </button> </form> My goal is to perform two actions w ...

Getting the value of a hidden input field within a div by accessing the event.target that initiated the event in JavaScript

I am working with a set of li elements, each containing specific child elements structured like this: <li class="list"> <!--Parent--> <input type="hidden" id="id" value="3"> <!--Child 1--> <div class="cd-1">....</div ...

Height and width not being properly set by Ngx-Bootstrap popover

Struggling to integrate ngx-bootstrap into my Ionic 2 / Angular 2 application. The popover seems to be having issues with its display in the view: <button placement="right" [outsideClick]="true" popover="this is a test text for the popover conten ...

Retrieving information from a JSON reply within an Angular 2 service

I am currently utilizing a service in angular 2 that retrieves JSON data from an API (). The code for fetching the data is depicted below: getImages() { return this.http.get(`${this.Url}`) .toPromise() .then(response => response.json() ...

Filtering aggregation after invoking $lookup

Is there a way to add a filter after performing an $lookup, or is there another method to achieve this? The data collection test looks like this: { "_id" : ObjectId("570557d4094a4514fc1291d6"), "id" : 100, "value" : "0", "contain" : [ ] } { "_id" : Objec ...

Tips for optimizing mobile performance by loading .obj models into objects on three.js

How can I properly load .obj models with objLoader and MTLLoader for my three.js mini game? I am facing an issue where the game loads fine on computers but fails to load on mobile browsers. Specifically, when accessed on a phone browser, the game attempts ...

Adjust the hue of the X axis labels to display a variety of colors within Chart.js

Utilizing Chart.js for my bar chart. The X axis labels contain 4 lines, and I want to change the color of each line individually rather than having all values in one color. var barChartData = { labels: [["Injection", 10, 20], // Change the color here ...

Developing JavaScript functionality to manage multiple radio buttons that should be hidden after being selected

I am currently in the process of building a comprehensive form that includes numerous radio buttons. My goal is to present one question at a time, which means using JavaScript to hide the respective div after a radio button has been clicked. While I have ...