Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Commit

Permalink
allow recursive subforms
Browse files Browse the repository at this point in the history
  • Loading branch information
jtjeferreira committed Nov 8, 2015
1 parent d8b0200 commit 5eb5683
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
4 changes: 2 additions & 2 deletions supler/src/main/scala/org/supler/SuplerFieldMacros.scala
Expand Up @@ -130,9 +130,9 @@ object SuplerFieldMacros {


def newSubformField[T, ContU, U, Cont[_]](c: SubformContainer[ContU, U, Cont]) def newSubformField[T, ContU, U, Cont[_]](c: SubformContainer[ContU, U, Cont])
(fieldName: String, read: T => Cont[U], write: (T, Cont[U]) => T, (fieldName: String, read: T => Cont[U], write: (T, Cont[U]) => T,
embeddedForm: Form[U], createEmpty: Option[() => U]): SubformField[T, ContU, U, Cont] = { embeddedForm: => Form[U], createEmpty: Option[() => U]): SubformField[T, ContU, U, Cont] = {


SubformField[T, ContU, U, Cont](c, fieldName, read, write, None, None, embeddedForm, createEmpty, SubformField[T, ContU, U, Cont](c, fieldName, read, write, None, None, () => embeddedForm, createEmpty,
SubformListRenderHint, AlwaysCondition, AlwaysCondition) SubformListRenderHint, AlwaysCondition, AlwaysCondition)
} }


Expand Down
4 changes: 3 additions & 1 deletion supler/src/main/scala/org/supler/field/SubformField.scala
Expand Up @@ -12,7 +12,7 @@ case class SubformField[T, ContU, U, Cont[_]](
write: (T, Cont[U]) => T, write: (T, Cont[U]) => T,
label: Option[String], label: Option[String],
description: Option[String], description: Option[String],
embeddedForm: Form[U], embeddedFormF: () => Form[U],
// if not specified, `embeddedForm.createEmpty` will be used // if not specified, `embeddedForm.createEmpty` will be used
createEmpty: Option[() => U], createEmpty: Option[() => U],
renderHint: RenderHint with SubformFieldCompatible, renderHint: RenderHint with SubformFieldCompatible,
Expand All @@ -21,6 +21,8 @@ case class SubformField[T, ContU, U, Cont[_]](


import c._ import c._


lazy val embeddedForm = embeddedFormF()

def label(newLabel: String): SubformField[T, ContU, U, Cont] = this.copy(label = Some(newLabel)) def label(newLabel: String): SubformField[T, ContU, U, Cont] = this.copy(label = Some(newLabel))
def description(newDescription: String): SubformField[T, ContU, U, Cont] = this.copy(description = Some(newDescription)) def description(newDescription: String): SubformField[T, ContU, U, Cont] = this.copy(description = Some(newDescription))


Expand Down
40 changes: 40 additions & 0 deletions supler/src/test/scala/org/supler/RecursiveTest.scala
@@ -0,0 +1,40 @@
package org.supler

import org.json4s.native._
import org.scalatest._
import Supler._

class RecursiveTest extends FlatSpec with ShouldMatchers {
case class Person(name: String, children: List[Person])

def personForm: Form[Person] = form[Person](f =>
List(
f.field(_.name),
f.subform((p: Person) => p.children, personForm)
)
)

"subform" should "apply json values to a list field" in {

val jsonInOrder = parseJson("""
|{
| "name": "n",
| "children": [
| {
| "name": "c1"
| },
| {
| "name": "c2"
| }
| ]
|}
""".stripMargin)

// when
val result = personForm(Person("", Nil)).applyJSONValues(jsonInOrder)

// then
result.errors should be ('empty)
result.obj should be (Person("n", List(Person("c1", Nil), Person("c2", Nil))))
}
}

0 comments on commit 5eb5683

Please sign in to comment.