Angular 5, reactive forms, nested components, error messages

Angular 5, reactive forms, nested components, error messages
0.0 0



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

<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

this.all_form ={ // <-- the parent FormGroup
          name: ['', Validators.required]

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

<span *ngIf="">Required Field</span>


<span *ngIf="">Required Field</span>


They give error

TypeError: is undefined


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

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


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.
If i have missed something tell me.


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:


so the template format is:

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

So for your example I would try

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

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)