Skip to content

Commit

Permalink
Form input helper argument "_error" should handle String (#9051)
Browse files Browse the repository at this point in the history
* Form input helper argument "_error" should handle String

* Tests
  • Loading branch information
mkurz authored and marcospereira committed Mar 1, 2019
1 parent 16e51c8 commit c5ebaf6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion core/play/src/main/scala/views/helper/Helpers.scala
Expand Up @@ -34,7 +34,9 @@ package views.html.helper {
(args.get('_error) match {
case Some(Some(play.api.data.FormError(_, message, args))) =>
Some(Seq(p.messages(message, args.map(a => translateMsgArg(a)): _*)))
case _ => None
case Some(Some(message: String)) => Some(Seq(p.messages(message)))
case Some(message: String) => Some(Seq(p.messages(message)))
case _ => None
}).getOrElse {
(if (args.get('_showErrors) match {
case Some(false) => false
Expand Down
26 changes: 26 additions & 0 deletions core/play/src/test/scala/views/html/helper/HelpersSpec.scala
Expand Up @@ -229,5 +229,31 @@ class HelpersSpec extends Specification {
"I am the name of the field"
)
}

"correctly display an error when _error is supplied as String" in {
inputText.apply(Form(single("foo" -> Forms.text))("foo"), '_error -> "Force an error").body must contain(
"""<dd class="error">Force an error</dd>"""
)
}

"correctly display an error when _error is supplied as Option[String]" in {
inputText.apply(Form(single("foo" -> Forms.text))("foo"), '_error -> Option("Force an error")).body must contain(
"""<dd class="error">Force an error</dd>"""
)
}

"correctly display an error when _error is supplied as Option[FormError]" in {
inputText
.apply(Form(single("foo" -> Forms.text))("foo"), '_error -> Option(FormError("foo", "Force an error")))
.body must contain(
"""<dd class="error">Force an error</dd>"""
)
}

"don't display an error when _error is supplied but is None" in {
inputText.apply(Form(single("foo" -> Forms.text))("foo"), '_error -> None).body must not contain (
"""class="error""""
)
}
}
}

0 comments on commit c5ebaf6

Please sign in to comment.