Jake
Jake

Reputation: 23

Angular 6 nested dynamic reactiveform

I am struggling to understand how to implement a nested ReactiveForm. I have an array of questions with an array of answers. Answers will be in a radio button group. However, I do not know how many questions nor how many answers, per question. I will retrieve from an API.

For each question, an answer is required.

e.g.

    <form class="form-group" id="{{prefix+'QuestionsForm'}}" [formGroup]="Form">
            <div class="form-group" id=" 
                {{prefix+'QuestionsAnswers'+'_'+i}}" *ngFor="let question of questions; index as i;">
                <div class="row">
                    <div class="col-xs-1 question-col">{{(i+1)+'. '}}</div>
                    <div id="{{prefix+'Question'+'_'+i}}" class="col-xs-11 no-padding-left>{{question.question}}</div>
                </div>
                <!--<div class="alert alert-danger gap-top-small alert-dismissible fade in experian" role="alert" style="display: block;">
                    <div class="glyphicon glyphicon-remove-sign color5 floatLeft"></div>
                    <div class="left-indent-text-block">Please select an option.</div>
                </div>-->
                <div class="row space-bottom-large">
                    <div class="col-xs-12 space-top-large">
                        <div class="radio-button space-top-small">
                            <div id="{{prefix+'Answers'+'_'+i}}" class="row space-left-large">
                                <div class="col-xs-12 space-bottom-large" *ngFor="let answer of question.answers; index as a">
                                    <input type="radio" name="{{'radio-group_'+i}}" id="{{prefix+'AnswerInput'+'_'+i+'_'+a}}" [value]="answer">
                                    <label id="{{prefix+'AnswerLabel'+'_'+i+'_'+a}}" for="{{prefix+'AnswerInput'+'_'+i+'_'+a}}">
                                        <span class="pull-left>
                                            {{answer}}
                                        </span>
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>

this.questions = [
    {
      question: "What is your favorite color?",
      answers: ["red", "blue", "green"]
    },
    {
      question: "What is your favorite animal?",
      answers: ["lion", "tiger", "dog", "cat"]
    },
    ..........
]

So I am thinking the ReactiveForm structure should be:

Form: FormGroup(
  Questions: FormArray([
    Answers: FormArray([], Validators.required)
  ])
)

OR should it be:

Form: FormGroup(
  Questions: FormArray([
    Question: "",
    Answer: FormControl(Validators.required)
  ])
)

Or some other structure?

I am not understanding which ReactiveForm structure I should use to construct the form to allow an unknown amount of questions and answers. but require each question to have an answer selected.

Your help is greatly appreciated.

Upvotes: 1

Views: 788

Answers (2)

Florian
Florian

Reputation: 267

The structure of the reactive form you are looking for is this:

FormGroup({
    Questions: FormArray([
        FormControl(Validators.required),
        FormControl(Validators.required),
        ...
    ])
})

Basically you want to add an entry for every question to the FormArray. Each entry consists of a FormControl for a radio button group. So all answers of a question share one FormControl.

I also made a Plunker example based of your requirements:

Angular 6 nested dynamic reactive form radio button example

You can log the form to the Plunker console and investigate it.

Hope this helps.

Upvotes: 2

Related Questions