0

First of all I just begin with Angular 2 and I'm trying to build a nested form and validate it and add new object Projects into object GroupProject.

Here's part of my ts file:

ngOnInit() {
  this.myForm = this.formBuilder.group({
    groupproject: this.formBuilder.array([
      this.initGroupProjects()
    ])
  });
}

public initGroupProjects(): any {
    return this.formBuilder.group({
        groupproject: this.formBuilder.array([this.initProjects()])
    });
}

public initProjects(): any {
  return this.formBuilder.group({
    name: ['', [Validators.required, Validators.minLength(3)]],
    some_array: this.formBuilder.array([
      this.formBuilder.group({
        name: ['', Validators.required],
        attr: ['', Validators.required],
        some_id: [1, Validators.required]
      })
    ])
  });
}

addProject(): void {
  const control = < FormArray > this.myForm.controls['projects'];
  control.push(this.initProjects());
}

View:

<form [formGroup]="myForm" novalidate (ngSubmit)="onSubmit(myForm)">
    <div formArrayName="groupproject">
        <div [formGroupName]="gp" *ngFor="let objGroupProject of myForm.controls.groupproject.controls; let gp = index">
            <div formArrayName="projects">
                <div [formGroupName]="i" *ngFor="let project of objGroupProject.controls.projects.controls; let i = index">
                    <md-input placeholder="Name" formControlName="name"></md-input>
                    <div formArrayName="some_array">
                        <div [formGroupName]="x" *ngFor="let some_obj of project.controls.some_array.controls; let x = index">
                            <div>
                                <md-input placeholder="Nome" formControlName="name"></md-input>
                                <small *ngIf="!some_obj.controls.name.valid">Nome é requerido</small>
                            </div>
                            <md-input type="number" placeholder="Cost" formControlName="attr" required></md-input>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <button type="submit" md-raised-button color="primary" [disabled]="!myForm.valid">Submit</button>
</form>
<pre>form value: <br>{{myForm.value | json}}</pre>

The output of form value:

form value:

{  
   "groupproject":[  
      {  
         "projects":[  
            {  
               "name":"",
               "some_array":[  
                  {  
                     "name":"",
                     "attr":"",
                     "some_id":1
                  }
               ]
            },
            {  
               "name":"",
               "some_array":[  
                  {  
                     "name":"",
                     "attr":"",
                     "some_id":1
                  }
               ]
            }
         ]
      }
   ]
}

Well, as you can see I have some arrays called groupprojects, with 1 array inside each one.

So the problem is that I'm not able to insert object project in object groupproject

Actually I'm getting the following error:

ERROR TypeError: Cannot read property 'controls' of undefined

1 Answer 1

1

Most likely you have a type in the following code:

public initGroupProjects(): any {
    return this.formBuilder.group({
        groupproject: this.formBuilder.array([this.initProjects()])
         ^^^^^^^
    });
}

I think it should be projects instead of groupproject:

public initGroupProjects(): any {
    return this.formBuilder.group({
        projects: this.formBuilder.array([this.initProjects()])
    });
}

Check also working Plunker Example

See also

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for answering. What I need is to add a new project object inside groupproject which is an array. Using the addProject method
I will review the following: Nested arrays in Angular 2 reactive forms?
@xlancelotx Please check this plunker plnkr.co/edit/uLHEp1rLUFKkbLVpsw2d?p=preview And consider accepting the answer if it helped, thanks
Thank you very much for replying @yurzui, is what I need to do. You gave me the exact answer thanks plnkr.co/edit/uLHEp1rLUFKkbLVpsw2d?p=preview

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.