Skip to content

Commit

Permalink
Merge pull request #159 from julienrf/lighthouse-189-patch
Browse files Browse the repository at this point in the history
Apply constraints on WrappedMapping
  • Loading branch information
guillaumebort committed Mar 5, 2012
2 parents 44e30d0 + dae9844 commit 5e306df
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
4 changes: 2 additions & 2 deletions framework/src/play/src/main/scala/play/api/data/Form.scala
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ case class WrappedMapping[A, B](wrapped: Mapping[A], f1: A => B, f2: B => A, val
* @return either a concrete value of type `B` or a set of errors, if the binding failed
*/
def bind(data: Map[String, String]): Either[Seq[FormError], B] = {
wrapped.bind(data).right.map(t => f1(t))
wrapped.bind(data).right.map(t => f1(t)).right.flatMap(applyConstraints)
}

/**
Expand All @@ -533,7 +533,7 @@ case class WrappedMapping[A, B](wrapped: Mapping[A], f1: A => B, f2: B => A, val
* @return either the plain data or a set of errors, if the unbinding failed
*/
def unbind(value: B): (Map[String, String], Seq[FormError]) = {
wrapped.unbind(f2(value))
(wrapped.unbind(f2(value))._1, collectErrors(value))
}

/**
Expand Down
45 changes: 45 additions & 0 deletions framework/src/play/src/test/scala/play/data/FormSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,51 @@ object FormSpec extends Specification {
myForm hasErrors () must beEqualTo(true)

}

"apply constraints on wrapped mappings" in {
import play.api.data._
import play.api.data.Forms._

val form = Form(
"foo" -> text.verifying("first.digit", s => (s.headOption map {_ == '3'}) getOrElse false)
.transform[Int](Integer.parseInt _, _.toString).verifying("number.42", _ < 42)
)

"when it binds data" in {
val f1 = form.bind(Map("foo"->"0"))
f1.errors.size must equalTo (1)
f1.errors.find(_.message == "first.digit") must beSome

val f2 = form.bind(Map("foo"->"3"))
f2.errors.size must equalTo (0)

val f3 = form.bind(Map("foo"->"50"))
f3.errors.size must equalTo (1) // Only one error because "number.42" can’t be applied since wrapped bind failed
f3.errors.find(_.message == "first.digit") must beSome

val f4 = form.bind(Map("foo"->"333"))
f4.errors.size must equalTo (1)
f4.errors.find(_.message == "number.42") must beSome
}

"when it is filled with data" in {
val f1 = form.fillAndValidate(0)
f1.errors.size must equalTo (1)
f1.errors.find(_.message == "first.digit") must beSome

val f2 = form.fillAndValidate(3)
f2.errors.size must equalTo (0)

val f3 = form.fillAndValidate(50)
f3.errors.size must equalTo (2)
f3.errors.find(_.message == "first.digit") must beSome
f3.errors.find(_.message == "number.42") must beSome

val f4 = form.fillAndValidate(333)
f4.errors.size must equalTo (1)
f4.errors.find(_.message == "number.42") must beSome
}
}
}

"render form using field[Type] syntax" in {
Expand Down

0 comments on commit 5e306df

Please sign in to comment.