# Angular 5, reactive forms, nested components, error messages

**URL:** <https://forum.freecodecamp.org/t/angular-5-reactive-forms-nested-components-error-messages/183672>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [April 4, 2018, 6:40pm UTC](https://forum.freecodecamp.org/t/angular-5-reactive-forms-nested-components-error-messages/183672 "2018-04-04T18:40:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![malice](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@malice](https://forum.freecodecamp.org/u/malice)\
**Post date:** [April 4, 2018, 6:40pm UTC](https://forum.freecodecamp.org/t/angular-5-reactive-forms-nested-components-error-messages/183672/1 "2018-04-04T18:40:24Z")

</div>

Hello,

I am creating a project with a big form that i would like to break on different components.  
I used an @Input in the child form component and pass it from the father by using [formGroup]=“parentFormGroup” in the child like bellow

```auto
<div class="form-group" [formGroup]="parentFormGroup">
  <div class="form-group" formGroupName="design">
    <div class="form-group" formGroupName="level1">
      <input class="form-control" formControlName="name"/>

```

The form is defined in the parent

```auto
this.all_form = this.fb.group({ // <-- the parent FormGroup
      design: this.fb.group({
        level1: this.fb.group({
          name: ['', Validators.required]
        })
      })
    })

```

Now on the child template i want to print an error message but nothing works. I tried

`<span *ngIf="parentFormGroup.controls.name.errors.required">Required Field</span>`

and

`<span *ngIf="parentFormGroup.design.level1.controls.name.errors.required">Required Field</span>`

and

`parentFormGroup.design.level1.controls.get('name').hasError('required')`

They give error

`TypeError: _co.parentFormGroup.design is undefined`

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [April 4, 2018, 7:01pm UTC](https://forum.freecodecamp.org/t/angular-5-reactive-forms-nested-components-error-messages/183672/2 "2018-04-04T19:01:33Z")

</div>

Can you share live code (in something like stackblitz or plunkr)?

Trying to describe something like this is really hard without the actual code.

---

<div class="post-metadata">

**Author:** ![malice](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@malice](https://forum.freecodecamp.org/u/malice)\
**Post date:** [April 4, 2018, 7:21pm UTC](https://forum.freecodecamp.org/t/angular-5-reactive-forms-nested-components-error-messages/183672/3 "2018-04-04T19:21:12Z")

</div>

Here is the code, not working cause the structure is wrong and i have not imported the libs but still all the files are there.

[https://plnkr.co/93ZR64PXATPCKpHEaeZN](https://plnkr.co/93ZR64PXATPCKpHEaeZN)  
If i have missed something tell me.

---

<div class="post-metadata">

**Author:** ![bradtaniguchi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bradtaniguchi/32/110427_2.png) [@bradtaniguchi](https://forum.freecodecamp.org/u/bradtaniguchi)\
**Post date:** [April 4, 2018, 11:21pm UTC](https://forum.freecodecamp.org/t/angular-5-reactive-forms-nested-components-error-messages/183672/4 "2018-04-04T23:21:41Z")

</div>

First off, nested form groups in the same component get very nasty very fast. If you can break down your formGroups into sub-components, so each components is concerned with only 1 level of the formGroup.  
Or you can use a middle step where you get the formValues and map it to the full object format, instead of trying to warp the formGroup into the object you want in the end.

Or you can just simplify your form entirely.

Regardless the format to show/hide errors is:

```auto
formGroup.controls['controlName'].hasError('required')

```

so the template format is:

```auto
<span *ngIf="formGroup.controls[controlName].hasError('required')">

```

So for your example I would try

```auto
<span *ngIf="parentFormGroup.controls['design'].controls['level1'].controls['name'].hasError('required')">
</span>

```

As you can see the syntax starts getting pretty nasty.

Here’s an example stackBlitz I created to check on the syntax. (Use stackBlitz not plunker, its faster more stable)  
[https://stackblitz.com/edit/angular-zsupuy](https://stackblitz.com/edit/angular-zsupuy)

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 7:30am UTC](https://forum.freecodecamp.org/t/angular-5-reactive-forms-nested-components-error-messages/183672/5 "2021-01-17T07:30:29Z")

</div>

Closed on January 17, 2021, 7:30am UTC


