Play Framework 2.1 /表单映射与复杂对象

Mik*_*378 3 forms scala playframework-2.1

Scala的Play Framework文档显示了一个隐式地将表单映射到案例类的示例:

case class User(name: String, age: Int)

val userForm = Form(
  mapping(
    "name" -> text,
    "age" -> number
  )(User.apply)(User.unapply)
)
Run Code Online (Sandbox Code Playgroud)

我们注意到在这个独特的样本中只使用了原始值.

如果我们做这个改动怎么样:

case class Car(brandName: String)

case class User(name: String, car: Car)
Run Code Online (Sandbox Code Playgroud)

而且,让我们假设表单返回User的名称(String)和一个carId(String)

val userForm = Form(
  mapping(
    "name" -> text,
    "car" -> carRepository.findById(nonEmptyText)  // concept I wish
  )(User.apply)(User.unapply)
)
Run Code Online (Sandbox Code Playgroud)

有没有办法在这个希望的线路上实例化一辆汽车,carId例如表格提供的一些汽车并确保它carId不是空的String

tra*_*ode 5

对于问题的第一部分,文档还显示了嵌套值:

case class Car(brandName: String)
case class User(name: String, car: Car)

val userForm = Form(
  mapping(
    "name" -> text,
    "car" -> mapping(
        "brandName" -> text
    )(Car.apply)(Car.unapply)
  )(User.apply, User.unapply)
)
Run Code Online (Sandbox Code Playgroud)


EEC*_*LOR 5

您可以提供Formatter并使用该of[Car]方法.

implicit val carFormat = new Formatter[Car] {
  def bind(key: String, data: Map[String, String]):Either[Seq[FormError], Car] = 
    data.get(key)
      // make sure the method returns an option of Car
      .flatMap(carRepository.findByBrandName _)
      .toRight(Seq(FormError(key, "error.carNotFound", Nil)))

  def unbind(key: String, value: Car) = Map(key -> value.brandName)
}
Run Code Online (Sandbox Code Playgroud)

这个答案提供了另一个Formatter:Play 2 - Scala - 表单验证器和单选按钮

然后你可以像这样使用它:

val userForm = Form(
  mapping(
    "name" -> text,
    "car" -> of[Car]
  )(User.apply)(User.unapply)
)
Run Code Online (Sandbox Code Playgroud)