游戏2:惯用的方法结合形式列出[产品型号] [英] Play 2: idiomatic approach to binding a form to List[Model]

查看:126
本文介绍了游戏2:惯用的方法结合形式列出[产品型号]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个CRUD操作执行,每一个对模型的集合(如比赛日程,球队花名册,比赛结果,技术统计数据,等等)。

I have several CRUD operations to perform, each one on a collection of models (e.g. game schedule, team roster, game result, game stats, etc.).

最多在我玩的经验,这点(短短数月,一期工程现场)我一直在使用一到一种形式绑定到模型实例。

Up to this point in my Play experience (just a few months, 1 project live) I have been working with one-to-one form binding to model instance.

我知道我可以用数字指标表单字段名称,但随后如何绑定张贴的形式列出[产品型号]?

I know I can numerically index form field names, but then how to bind the posted form to List[Model]?

这是我的一到一个绑定的样子:

This is what my one-to-one binding looks like:

// abstract away bindFromRequest to make binding more concise in controllers
def bindForm[T](f: play.api.data.Form[T])(implicit r: play.api.mvc.Request[_]) =
  f.bindFromRequest fold(e=> Left(e.errorsAsJson), Right(_))

,然后在控制器:

and then in controllers:

val result = for {
  model <- bindForm(form).right
  id    <- dao.create(model) as json
} yield id

我想要做的是一样的,但不是模型绑定在成功时返回一个单一的模式,有它返回一个列表[产品型号],并传递到重载DAO创建/编辑/删除操作。

what I would like to do is the same, but instead of model binding returning a single Model on success, have it return a List[Model], and pass on to overloaded DAO create/edit/delete operations.

我看到有一个列表方法,它可以作为一个表映射的一部分使用,但我有一种感觉,这将严重破坏我的JDBC查询包装(ScalaQuery /油滑),其案例类/伴侣对象映射可能不会与集合的属性打好。

I see that there is a list method that one can use as part of a Form mapping, but I have a feeling that that would wreak havoc with my JDBC query wrapper (ScalaQuery/Slick), whose case class/companion object mapping would likely not play well with collections properties.

例如,游戏进度的现有映射如下:

For example, existing mapping of a game schedule looks like:

object CompositeForm {
  import play.api.data.{Form, Forms}, Forms._
  import utils.Validator.Bindings.jodaLocalTimeFormat
  val mapper = mapping(
    'id   -> ignored(0),
    'gameDate -> jodaDate,
    'gameType -> optional(text),
    'location -> optional(text),
    'team1    -> number,
    'team2    -> number
  )(Composite.apply)(Composite.unapply)
  val form = Form( mapper )
}

使用列表(gameDate),列表(游戏类型)而不是那么意味着形式结合会返回一个复合实例,其属性是所有集合 - 也许它会工作,但似乎并不几乎一样干净/简单与模型实例的收集。

using list(gameDate), list(gameType) instead then means that form binding will return a single Composite instance whose properties are all collections -- maybe it will work, but doesn't seem nearly as clean/straightforward as working with a collection of model instances.

理念AP preciated; - )

Ideas appreciated ;-)

推荐答案

在尚未证明 SEQ()在游戏的形式映射选项是向我指出的播放谷歌通过@Julien理查德·富瓦集团

The as yet documented seq() option in play form mapping was pointed out to me on Play google group by @Julien Richard-Foy

使用重复() SEQ()一起允许一个重复的表单映射,从而创建一个集合索引foo.bar [n]的formfield元素。

Using repeat() and seq() together allows one to repeat a form mapping, thus creating a collection of indexed foo.bar[n] formfield elements.

示例

object ScheduleForm {
  import play.api.data.{Form, Forms}, Forms._
  val mapper = mapping(
    'composite -> seq(CompositeForm.mapper), 
    'note -> seq(ScheduleNoteForm.mapper)
  )(Schedule.apply)(Schedule.unapply)
  val form = Form( mapper )
}

,然后在一个视图:

and then in a view:

@repeat(_form("composite"), min=@numGames) { f=>
    @inputDate(f("gameDate"), '_label-> "Game Date", 'class-> "required")
...
}

这篇关于游戏2:惯用的方法结合形式列出[产品型号]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆