I need to present a set of questions fetched from an API, and having trouble displaying the form in the template, and getting the correct values of the user's response. The format of the APIs response is the following:
{
questions: [
{
text: 'Is this a question?',
id: 'question1',
answers: [
{ text: 'Yes', id: 'answer1' },
{ text: 'No!', id: 'answer2' },
{ text: 'Don't know?', id: 'answer3' }
]
}
]
}
I have created a form which looks like the following:
In the component
constructor(private fb: FormBuilder) {
this.questionsAnswersForm = this.fb.group({
answers: this.fb.array([])
});
}
this.initFormWithQAs()
initFormWithQAs(): void {
this.questions.map(() =>
(<FormArray>this.questionsAnswersForm.get('answers')).push(
this.addQuestionFormGroup()
)
);
}
addQuestionFormGroup(): FormGroup {
return this.fb.group({
questionId: [null, Validators.required],
answerId: [null, Validators.required]
});
}
In the template
<div formArrayName="answers" *ngFor="let q of questions">
<h5>{{ q.text }}</h5>
<form [formGroupName]="q.id" class="qa-list">
<label *ngFor="let answer of q.answers">
<input
type="radio"
[value]="answer.id"
[formControlName]="answer.id"
/>
<span>{{ answer.text }}</span>
</label>
</form>
</div>
The questionnaire renders on the screen with the correct text, but the radio buttons are not uniquely selected (working like checkboxes), the values on form submit are always null, and i am also getting the following error:
TypeError: Cannot create property 'validator' on string 'question1'
Thanks for the help in advance!