1

Form Control for Radio Group with same name

The below is the code of component

    bedsArray: Array<{displayText: string, bed: number}> = [];
    roomCount = 3; // This is not fixed.
    roomForm: FormGroup;

    ngOnInit() { 
    this.bedsArray.push({bed: 1, displayText: '1 Bed'});
    this.bedsArray.push({bed: 2, displayText: '2 Beds'});
    this.bedsArray.push({bed: 3, displayText: '3 Beds'});

    this.roomForm = new FormGroup({
      rooms: this.genRoomArray(),
      buildingName: new FormControl('Building 1')
    });
    }

    genRoomArray(): FormArray {
    const roomsArray = new FormArray([]);
    for(let i = 0; i < this.roomCount; i++) {
      const roomGroup = new FormGroup({
        roomNo: new FormControl('', Validators.required),
        bedsCount: new FormControl('', Validators.required)
      });

      roomsArray.push(roomGroup);
    }
    return roomsArray;
    }

and the code for the template

<form [formGroup]="roomForm">
  <h3> {{ roomForm.get('buildingName').value }} Rooms </h3>
  <div class="rooms" formArrayName="rooms">
    <div class="room" style="padding: 2rem; border:1px solid gray; margin- 
      bottom:1rem;" *ngFor="let room of roomForm.get('rooms').controls" 
      [formGroup] = "room">
         Room Number: <input type="text" formControlName="roomNo"> 
         <div class="beds">
            <div class="bed" *ngFor="let bed of bedsArray" [formGroup] = "room">
            <input [value] = "bed.bed" name="bedsCount" formControlName="bedsCount" type="radio">       
            {{ bed.displayText }}
       </div>
     </div>
    </div>
   </div>
  </form>

The radio buttons doesn't work as expected. Only one radio button gets selected.

The html name should be different for the radio buttons to work correctly. How do I do that.

Demo

3
  • You are missing name attribute and [value] in your radio button. But, I don't think you even want a formarray here? Now you are pushing a new formgroup for all possibilites, I guess you just want a single value. Commented Feb 2, 2019 at 14:23
  • I have added those now.. It's still the same. I need the formArray because there are multiple room displayed in one page. the code i have given above will come inside an another loop Commented Feb 2, 2019 at 14:27
  • html example This is what i am looking for. How do I do this kind of radio buttons in reactive forms. I know that the name has to differ for each radio group. Commented Feb 2, 2019 at 14:40

1 Answer 1

2

So your template is aaaaalmost alright, we need a dynamic id instead of name, so do the following small changes (in comment) also stripped away noise just for the answer :

<form [formGroup]="roomForm">
  <div formArrayName="rooms">
    <div *ngFor="let room of roomForm.get('rooms').controls" [formGroup]="room">
     Room Number: <input type="text" formControlName="roomNo"> 
     <!-- You can remove formgroup from below div --> 
     <!-- add index so we have a dynamic index for id -->
      <div *ngFor="let bed of bedsArray; let i = index">
        <input [value]="bed.bed" [id]="i" formControlName="bedsCount" type="radio">
        {{ bed.displayText }}
     </div>
    </div>
  </div>
</form>

DEMO

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

12 Comments

Thank you for the answer. although, this is not what i am looking for. There are multiple room displayed in one page so i will have to use the form array. codepen.io/anon/pen/ZwKvov is what i am looking for.
Then show a minimal reproducible example, there is not enough code to recreate the issue, you are not showing the build of the form or the whole minimal template to recreate the issue. Take the stackblitz and recreate the issue there with your code and I'm glad to help!
The same example which you have done. Just the possibility for multiple rooms in a single form.
Yes, I know what you mean, and I could do it for you, but please make an effort yourself. I want this, do it for me isn't what StackOverflow is for ;) I rather look at your code, explain what is wrong and in that way help you.
I have modified the entire question and attached a demo. Kindly help me with the my code.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.