【问题标题】:Play (Scala) Custom form type validationPlay (Scala) 自定义表单类型验证
【发布时间】:2013-07-12 23:48:51
【问题描述】:

在我看来,我正在创建一个单选按钮,其值为 0,1,2(字符串)。

inputRadioGroup(
  dayForm("time"),
  options("0"->"Morning","1"->"Afternoon","2"->"Night"))

在我的模型中,我使用时间作为整数值,而单选框没有将输入作为整数提交的选项。

所以在我的控制器中,我将时间输入转换为整数并检查值是否在 0-2 之间。

val dayForm = Form(
  mapping(
    "id" -> optional(longNumber),
    "time" -> <what can I  do here to avoid type mismatch?>,
    "date" -> sqlDate("yyyy-MM-dd") (Entry.apply)(Entry.unapply)

有没有办法将时间转换为整数,然后验证它是否是 0-2 之间的数字?像这样:

"time" -> number (min =0, max =2)

这将导致表单无法验证,因为输入字符串尚未转换。

【问题讨论】:

    标签: scala playframework playframework-2.0


    【解决方案1】:

    我觉得你会写

    val dayForm = Form(
        mapping(
        "id" -> optional(longNumber),
        "time" -> number (min =0, max =2),
        "date" -> sqlDate("yyyy-MM-dd")
        )((id, time, date) => Entry(id, convertFunction(time), date))
        ((entry: Entry) => Some(entry.id, inverseFunction(entry.time), entry.date))
    )
    

    【讨论】:

      猜你喜欢
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多