0

I have a reactive form that uses arrays and has this structure:

 this.myForm = this.fb.group({
  names: this.fb.array([
    this.fb.group({ element_name: [null, [Validators.required]] }),
    this.fb.group({ element_name: [null, [Validators.required]] }),
  ])

enter image description here

Everything works well, performs the respective validations.

I want to add the lastname in another text field and have the required validation, but I do not know what is the way to accommodate like in the image:

enter image description here

I tried to do something similar like what I did with "names" but it doesn't make a mistake. What I can do?

this is my live code:

https://stackblitz.com/edit/angular-nm2vah?file=app/app.component.html

this is my code:

<form [formGroup]="myForm">
  <div formArrayName="names">
      <ng-container
          *ngFor="let item of myForm.get('names').controls; let i=index">
          <div [formGroupName]="i">
                  name <input type="text" class="form-control" id="element_name"
                      formControlName="element_name"
                      placeholder="insert name" 
                  >
                  <!--lastname 
                  <input type="text" class="form-control" id="element_lastname"
                      formControlName="element_lastname"
                      placeholder="insert last name" 
                  >-->
          </div>
      </ng-container>
  </div>
</form>


this.myForm = this.fb.group({
  names: this.fb.array([
    this.fb.group({ element_name: [null, [Validators.required]] }),
    this.fb.group({ element_name: [null, [Validators.required]] }),
  ]),
  lastname: this.fb.array([
    this.fb.group({ element_name: [null, [Validators.required]] }),
    this.fb.group({ element_name: [null, [Validators.required]] }),
  ])

})
1

1 Answer 1

2

You need to add last name to same form group of the firt name.

this.myForm = this.fb.group({
  names: this.fb.array([
    this.fb.group({
      element_name: [null, [Validators.required]],
      element_lastname: [null, [Validators.required]]
    }),
    this.fb.group({
      element_name: [null, [Validators.required]],
      element_lastname: [null, [Validators.required]]
    }),
  ]),
})

And Your Html:

<form [formGroup]="myForm">
  <div formArrayName="names">
      <ng-container
          *ngFor="let item of myForm.get('names').controls; let i=index">
          <div [formGroupName]="i">
                  name <input type="text" class="form-control" id="element_name"
                      formControlName="element_name"
                      placeholder="insert name" 
                  >

                  <input type="text" class="form-control" id="element_lastname"
                      formControlName="element_lastname"
                      placeholder="insert last name" 
                  >
          </div>
      </ng-container>
  </div>
</form>
Sign up to request clarification or add additional context in comments.

Comments

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.