我想将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())}
如果我用一些替换Option:
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中创建了一个包含所需依赖项的简单项目,你可以在这里下载并查看它,如果它有帮助: Basic Form Example
答案 0 :(得分: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)))
我将circumflex添加到依赖项并尝试了您的确切示例。它编译好我,我刚刚添加
object Task extends Task with Table[Long, Task]
我相信你忘了将它包含在问题中。所以我只能建议清理和重建整个项目。
P.S。我改变了行
{ (name, description) => new Task(name, description) }
但很明显。
答案 1 :(得分:0)
主要问题是如果使用circumflex,则不会编写case类,因此默认情况下没有apply和unapply方法。
您必须在Task配对对象中编写自己的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())
}