Reputation: 15435
I have the following checkbox defined in my html template:
<div class="answerDiv">
@if(exam.currentQuestion.get.quesType == "RADIO_BUTTON") {
<p>
@for(answer <- exam.currentQuestion.get.answers) {
<input type="radio" id="@answer.id" name="answerRadio" value="@answer.id"> @answer.text</input><br>
}
</p>
}
@if(exam.currentQuestion.get.quesType == "CHECK_BOX") {
<p>
@for(answer <- exam.currentQuestion.get.answers) {
<input type="checkbox" name="answerCheckbox" value="@answer.id"> @answer.text</input><br>
}
</p>
}
</div>
I would want the value of the checkbox in my controller where I have a form defined:
val runExamForm = Form(
tuple(
"currentQuestionId" -> text,
"examId" -> text,
"totalQuestions" -> number,
"answerRadio" -> optional(number),
"answerCheckBox" -> optional(list(number))
)
)
In my action I'm fetching the form values, but the value for the answerCheckBox is always None. Why is this? What am I doing wrong?
Upvotes: 4
Views: 4390
Reputation: 15435
Ok. I got it to work. Here is what I did in my view template:
<div class="answerDiv">
@if(exam.currentQuestion.get.quesType == "RADIO_BUTTON") {
<p>
@for(answer <- exam.currentQuestion.get.answers) {
<input type="radio" name="answerRadio" value="@answer.id"> @answer.text</input><br>
}
</p>
}
@if(exam.currentQuestion.get.quesType == "CHECK_BOX") {
<p>
@for(answer <- exam.currentQuestion.get.answers) {
<input type="checkbox" id="@answer.id" name="answerCheckbox[@answer.id]" value="@answer.id"> @answer.text</input><br>
}
</p>
}
</div>
Notice the name attribute in the checkbox html rendering. It has to be an array for the binding in my controller to work.
Upvotes: 3