10

Practicing what is written here: ScalaForms, I have created the following form:

  val personCreationForm = Form(
    tuple (
        "name" -> nonEmptyText,
        "age" -> number verifying (min(0), max(100))       /*ERROR*/
    ) verifying ("Wrong entry", result => result match {
      case (name, age) => true
    })
  )

However, the error on verifying states that value verifying is not a member of (java.lang.String, play.api.data.Mapping[Int]).

Working with mapping instead of tuple, as in the referenced example, makes no difference. What is wrong here?

noncom
  • 4,962
  • 3
  • 42
  • 70

1 Answers1

9

According to Scala operators precedence rules, methods starting with a letter have a lower precedence than others so when you write:

"age" -> number verifying (min(0), max(100))

The compiler builds the following expression:

("age" -> number) verifying (min(0), max(100))

Which is not what you want! You can rewrite it as follows:

"age" -> number.verifying(min(0), max(100))
"age" -> (number verifying (min(0), max(100)))

And the current Play documentation is wrong. Thanks for catching it!

Community
  • 1
  • 1
Julien Richard-Foy
  • 9,634
  • 2
  • 36
  • 42
  • Two years ago and the documentation is still wrong. It also doesn't seem to solve the "name" -> text verifying(required) documentation compile error. – halt00 Aug 11 '14 at 13:16
  • The up to date documentation is correct: https://www.playframework.com/documentation/2.3.x/ScalaForms – Julien Richard-Foy Aug 16 '14 at 21:52