Search code examples
formsscalaormplayframework-2.0circumflex-orm

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


Solution

  • 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.

    P.S. and I changed line

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

    but it is obvious.