播放框架复选框表单绑定

时间:2013-12-29 06:47:29

标签: scala checkbox playframework-2.1

我的html模板中定义了以下复选框:

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

我希望我的控制器中的复选框的值有一个定义的表单:

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

在我的操作中,我正在获取表单值,但answerCheckBox的值始终为None。为什么是这样?我做错了什么?

1 个答案:

答案 0 :(得分:3)

确定。我得到了它的工作。这是我在视图模板中所做的:

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

注意复选框html渲染中的name属性。它必须是我的控制器中绑定工作的数组。