Pergunta

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 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?

Foi útil?

Solução

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">&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 checkbox html rendering. It has to be an array for the binding in my controller to work.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top