简体   繁体   中英

provide own unapply method in play framework 2 with scala and circumflex-orm class

I want to combine the form binding from play 2.0 framework with a class extending Record from the circumflex-orm ( website ).

These are my class objects:

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
}

And this is what i try to do with the play form:

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

But i get an error like this:

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

And if i replace Option by Some:

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

I am clueless right now and any hint would be appreciated.

Thanks a lot.

EDIT: I made a basic error, i did name the Form:

val taskForm: Form[Tasks] = Form(

when the name of the class is "Task". So i can change it to:

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

And now i get a different error:

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

I made a simple project with the needed dependencies in eclipse, you can download it here and look at it, if it helps: Basic Form Example

I was wrong in comment, following snippet works for me.

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)))

Update

I added circumflex to dependencies and tried your exact example. It compiles fine for me, I just added

object Task extends Task with Table[Long, Task]

I believe you forget to include it in the question. So I can only suggest to clean and rebuild entire project.

PS and I changed line

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

but it is obvious.

The main problem is if you use circumflex you don't write case classes so you don't have apply and unapply methods by default.

You have to write your own apply and unapply methods in your Task companion object like this:

    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())
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM