Permalink
Browse files

allow recursive subforms

  • Loading branch information...
jtjeferreira committed Nov 8, 2015
1 parent d8b0200 commit 5eb5683b6f0935e8931cb06fdb1831033727dc39
@@ -130,9 +130,9 @@ object SuplerFieldMacros {
def newSubformField[T, ContU, U, Cont[_]](c: SubformContainer[ContU, U, Cont])
(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)
}
@@ -12,7 +12,7 @@ case class SubformField[T, ContU, U, Cont[_]](
write: (T, Cont[U]) => T,
label: Option[String],
description: Option[String],
embeddedForm: Form[U],
embeddedFormF: () => Form[U],
// if not specified, `embeddedForm.createEmpty` will be used
createEmpty: Option[() => U],
renderHint: RenderHint with SubformFieldCompatible,
@@ -21,6 +21,8 @@ case class SubformField[T, ContU, U, Cont[_]](
import c._
lazy val embeddedForm = embeddedFormF()
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))
@@ -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.