Say I have code like this:
case class userForm(username: String, password: String)
implicit val userFormReads: Reads[userForm] = (
(JsPath \ "username").read[String](email) and
(JsPath \ "password").read[String](minLength[String](6))
)(userForm.apply _)
val userFormResult: JsResult[userForm] = request.body.validate[userForm](userFormReads)
userFormResult match {
case e: JsError =>
new JsonResult("error", Messages(e.errors(0)._2(0).message)).send
case s: JsSuccess[userForm] =>
// process form .....
If I get something like "error.email", everything works great because in my messages.en-US I have defined error.email=Please enter a valid email address.
However if the 2nd validation fails (the one on password minlength 6), then I get: Minimum length is {0} as the result
What's the best way to handle validation errors in a humanly readable format? I need to pass something like this to the frontend to show our standard error message:
{"errors": true, message: "Please enter a valid password that is longer than 6 characters"}
Basically...it would be great to force a certain error message if any of the validations fail. Something like this would be awesome:
implicit val userFormReads: Reads[userForm] = (
(JsPath \ "username").read[String](email).getOrElse("Please enter a valid Username") and
(JsPath \ "password").read[String](minLength[String](6)).getOrElse("Please enter a valid Password")
)(userForm.apply _)
Thanks!