1

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.

2 Answers 2

2

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.

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

1 Comment

I appreciate your time and the answer. I figured out the structure this morning, prior to seeing this. I believe getting away from the code for awhile helped my thought process :P
0

you have to create a dynamic component to display the question list

https://angular.io/guide/dynamic-form

1 Comment

I understand I need to create a dynamic form. however, the question is, what is the ReactiveForm structure?

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.