1

我正在开发 Scala Play 2.7.x(您可以在此处查看项目play-silhouette-seed googleauth branch),并且我的表单定义为:

object TotpSetupForm {
  val form = Form(
    mapping(
      "sharedKey" -> nonEmptyText,
      "scratchCodes" -> seq(mapping(
        "hasher" -> nonEmptyText,
        "password" -> nonEmptyText,
        "salt" -> optional(nonEmptyText)
      )(PasswordInfo.apply)(PasswordInfo.unapply)),
      "scratchCodesPlain" -> optional(seq(nonEmptyText)),
      "verificationCode" -> nonEmptyText(minLength = 6, maxLength = 6)
    )(Data.apply)(Data.unapply)
  )

  case class Data(
    sharedKey: String,
    scratchCodes: Seq[PasswordInfo],
    scratchCodesPlain: Option[Seq[String]],
    verificationCode: String = "")
}

Play-SilhouettePasswordInfo来自哪里,看起来像:

case class PasswordInfo(
  hasher: String,
  password: String,
  salt: Option[String] = None
) extends AuthInfo    

在我的控制器中,我填充表单并将其作为参数传递给我的视图模板,如下所示。请注意,我已经对其进行了调试,并且totpInfo.scratchCodes有 5 个值,并且表单已正确填充:

val formData = TotpSetupForm.form.fill(TotpSetupForm.Data(totpInfo.sharedKey, totpInfo.scratchCodes, totpInfo.scratchCodesPlain))
Ok(views.html.someView(formData, ...)

我将视图呈现如下,请注意我确实阅读了 Scala Forms Repeated Values 文档说明:)

@helper.form(action = controllers.routes.TotpController.submit()) {
    @helper.CSRF.formField
    @b3.text(totpForm("verificationCode"), '_hiddenLabel -> messages("verificationCode"), 'placeholder -> messages("verificationCode"), 'autocomplete -> "off", 'class -> "form-control input-lg")
    @b3.hidden(totpForm("sharedKey"))
    @helper.repeatWithIndex(totpForm("scratchCodes"), min = 1) { (scratchCodeField, index) =>
        @b3.hidden(scratchCodeField, '_label -> ("scratchCode #" + index))
    }
    <div class="form-group">
        <div>
            <button id="submit" type="submit" value="submit" class="btn btn-lg btn-primary btn-block">@messages("verify")</button>
        </div>
    </div>
}

即使正确填充了表单的 scratchCodes 序列,每个序列值仍呈现为空:

<input type="hidden" name="scratchCodes[0]" value="" >
<input type="hidden" name="scratchCodes[1]" value="" >
<input type="hidden" name="scratchCodes[2]" value="" >
<input type="hidden" name="scratchCodes[3]" value="" >
<input type="hidden" name="scratchCodes[4]" value="" >

不过,序列中的字段数是正确的。

我也尝试过使用@helper.repeat替代方法,甚至使用@helper.input代替@b3.hidden来确定结果总是相同的......我得到了空值PasswordInfo元组渲染。

我怎样才能解决这个问题?

4

1 回答 1

2

OK 找到了罪魁祸首:重复 + 嵌套值需要分别访问每个属性,如下所示:

@helper.repeat(totpForm("scratchCodes"), min = 1) { scratchCodeField =>
  @b3.hidden(scratchCodeField("hasher"))
  @b3.hidden(scratchCodeField("password"))
  @b3.hidden(scratchCodeField("salt"))
}

然后工作正常,发布请求PasswordInfo正确填充 UDT 序列。

于 2019-05-23T00:05:21.610 回答