10

也许我只是忽略了一些明显的东西,但我不知道如何将表单字段绑定到 Play 控制器中的双精度。

例如,假设这是我的模型:

case class SavingsGoal(
timeframeInMonths: Option[Int],
amount: Double,
name: String
)

(忽略我用双倍的钱,我知道这是个坏主意,这只是一个简化的例子)

我想像这样绑定它:

object SavingsGoals extends Controller {

    val savingsForm: Form[SavingsGoal] = Form(

        mapping(
            "timeframeInMonths" -> optional(number.verifying(min(0))),
            "amount" -> of[Double],
            "name" -> nonEmptyText
        )(SavingsGoal.apply)(SavingsGoal.unapply)

    )

}

我意识到这个number助手只适用于整数,但我认为使用of[]可能允许我绑定一个双精度数。但是,我得到一个编译器错误:

Cannot find Formatter type class for Double. Perhaps you will need to import
play.api.data.format.Formats._  

这样做无济于事,因为 API 中没有定义双重格式化程序。

这只是询问在 Play 中将表单字段绑定到双精度的规范方式是什么?

谢谢!

编辑:4e6 为我指明了正确的方向。这是我在他的帮助下所做的:

使用他链接中的片段,我将以下内容添加到 app.controllers.Global.scala:

object Global {

    /**
     * Default formatter for the `Double` type.
     */
    implicit def doubleFormat: Formatter[Double] = new Formatter[Double] {

      override val format = Some("format.real", Nil)

      def bind(key: String, data: Map[String, String]) =
        parsing(_.toDouble, "error.real", Nil)(key, data)

      def unbind(key: String, value: Double) = Map(key -> value.toString)
    }

    /**
     * Helper for formatters binders
     * @param parse Function parsing a String value into a T value, throwing an exception in case of failure
     * @param error Error to set in case of parsing failure
     * @param key Key name of the field to parse
     * @param data Field data
     */
    private def parsing[T](parse: String => T, errMsg: String, errArgs: Seq[Any])(key: String, data: Map[String, String]): Either[Seq[FormError], T] = {
      stringFormat.bind(key, data).right.flatMap { s =>
        util.control.Exception.allCatch[T]
          .either(parse(s))
          .left.map(e => Seq(FormError(key, errMsg, errArgs)))
      }
    }

}

然后,在我的表单映射中:

mapping(
    "amount" -> of(Global.doubleFormat)
)
4

2 回答 2

11

如果您有 2.1 版本,则不需要使用全局格式。

只需导入:

import play.api.data.format.Formats._

并用作:

mapping(
    "amount" -> of(doubleFormat)
)
于 2015-03-25T09:53:00.073 回答
10

实际上,主分支上预定义的格式化程序。Double因此,您应该切换到2.1-SNAPSHOT播放版本或只是复制实现。

于 2012-10-06T13:39:53.203 回答