@if(exam.currentQuestion.get.quesType == ...">

Flag Shape Lock

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">&nbsp;&nbsp;@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">&nbsp;&nbsp;@answer.text</input><br>
                }
            </p>
        }
    </div>

I need the checkbox value in my controller where I defined the form:

  val runExamForm = Form(
    tuple(
      "currentQuestionId" -> text,
      "examId" -> text,
      "totalQuestions" -> number,
      "answerRadio" -> optional(number),
      "answerCheckBox" -> optional(list(number))
    )
  )

In my action, I retrieve the form values, but the value for answerCheckBox is always None. Why is this? What am I doing wrong?

+4
source share
1 answer

Ok I have earned. 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">&nbsp;&nbsp;@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">&nbsp;&nbsp;@answer.text</input><br>
                }
            </p>
        }
    </div>

Notice the name attribute in the html checkbox rendering. It must be an array to bind my controller to work.

+3
source

All Articles