0

我想将 play 2.0 框架中的表单绑定与从circumflex-orm ( website ) 扩展 Record 的类结合起来。

这些是我的类对象:

class Task extends Record[Long, Task] with IdentityGenerator[Long, Task] {  
  def this(name: String, description: String) = {
    this()
    this.name := name
    this.description := description
}

  val id = "id".BIGINT.NOT_NULL.AUTO_INCREMENT
  val name = "name".VARCHAR(255).NOT_NULL
  val description = "description".TEXT.NOT_NULL

  def PRIMARY_KEY = id
  def relation = Task
}

这就是我试图用播放形式做的事情:

val taskForm: Form[Tasks] = Form(
  mapping(
    "name" -> text,
    "description" -> text
  )
  {(name, description) => Task(name, description)}
  {(t: Task) => Option(t.name(), t.description())  }
)

但我收到这样的错误:

found   : models.Task => Option[(String, String)]
required: Unit => Option[(String, String)]
  {(t: Task) => Option(t.name(), t.description())}

如果我用一些替换选项:

found   : models.Task => Some[(String, String)]
required: Unit => Option[(String, String)]
  {(t: Task) => Some(t.name(), t.description())}

我现在一无所知,任何提示将不胜感激。

非常感谢。

编辑:我犯了一个基本错误,我确实命名了表格:

val taskForm: Form[Tasks] = Form(

当类的名称是“任务”时。所以我可以将其更改为:

val taskForm: Form[Task] = Form(
  mapping(
      "name" -> text,
      "description" -> text
  ) ( (name, description) => Task ) 
  ( (t: Task) => Option() )
)

现在我得到一个不同的错误:

Unspecified value parameter x
  ( (t: Task) => Option() )

我在eclipse中做了一个需要依赖的简单项目,你可以在这里下载并查看它,如果有帮助: 基本表单示例

4

2 回答 2

2

我的评论错了,下面的片段对我有用。

case class Foo(x: String, y: String)

val taskForm = Form(
  mapping(
    "name" -> text,
    "description" -> text)
  ((name, description) => Foo(name, description))
  ((t: Foo) => Some(t.x, t.y)))

更新

我在依赖项中添加了抑扬符,并尝试了您的确切示例。它对我来说编译得很好,我只是添加了

object Task extends Task with Table[Long, Task]

我相信您忘记将其包含在问题中。所以我只能建议清理和重建整个项目。

PS,我换了行

{ (name, description) => new Task(name, description) }

但很明显。

于 2012-04-19T17:31:08.123 回答
0

主要问题是如果您使用circumflex,您不会编写案例类,因此默认情况下您没有 apply 和 unapply 方法。

你必须在你的任务伴生对象中编写自己的应用和取消应用方法,如下所示:

    object Taks extends Task with Table[Long, Task] {
        def apply(name:String,description:String) = {
            var t = new Task()
            t.name := name
            t.description := description
            t
        }
        def unapply(t:Task) = Some(t.name(),t.description())
    }
于 2012-08-14T10:05:29.803 回答