Reputation: 323
I'm currently fighting with Scala constraints. I have a case class to bind the form to and a mapping function.
But i don't know how to verify two fields by comparing them (the first should be greater than the second)
Currently I'm thinking it should be done like this:
models/JobRequest.scala
package models
case class AddJobRequest (
exrange: Boolean,
exrangefrom: Int,
exrangeto: Int
)
controllers/Index.scala
package controllers
/* Code code code */
val jobAddForm = Form(
mapping(
"exrange" -> boolean,
tuple(
"exrangefrom" -> number(min = 7, max=19),
"exrangeto" -> number(min = 8, max = 20)
).verifying("Start number is greater than the stop number!", /** MAGIC GOES HERE */)
)(AddJobRequest.apply)(AddJobRequest.unapply))
Is there are a possibility to check, if exrangefrom
is greater than exrangeto
? Or is that a completely bad way to check that using ad-hoc constraints?
Upvotes: 2
Views: 960
Reputation: 302
You can use verifying
on the mapping(...)
object ! In your case, comparing exrangefrom
and exrangeto
could be done this way :
package controllers
/* Code code code */
val jobAddForm = Form(
mapping(
"exrange" -> boolean,
"exrangefrom" -> number(min = 7, max=19),
"exrangeto" -> number(min = 8, max = 20)
)(AddJobRequest.apply)(AddJobRequest.unapply)
verifying(
"Start number is greater than the stop number!",
addJobRequest => addJobRequest.exrangefrom < addJobRequest.exrangeto
)
)
Hope it's what you expected ;)
Upvotes: 3