I would like form validation in Play framework to produce a variable error message depending on collateral results of the validation. How can I do this cleanly?
Specific example — Say I have a form defined thus:
val f = Form(
"xml" -> text
.verifying(
"xml is incorrect",
xml => validationError(xml) == None
)
)
def validationError(xml: String): Option[String]
What can I do to show the string returned by validationError as the validation error message, instead of the fixed string “xml is incorrect”?
You can create
Constraintinstance which will handle it. For example:Also, there is another
applymethod onConstraintobject without specifying the name of constraint.