Skip to content

Instantly share code, notes, and snippets.

@vdebergue
Created June 12, 2014 15:51
Show Gist options
  • Select an option

  • Save vdebergue/6e3ddbae58ba83937245 to your computer and use it in GitHub Desktop.

Select an option

Save vdebergue/6e3ddbae58ba83937245 to your computer and use it in GitHub Desktop.
validate two dependants fields with play framework
val locationReadsCreate = new Reads[(String, Option[String])] {
def reads(json: JsValue) = {
val subsidiary = (json \ "subsidiary").asOpt[String]
val location = (json \ "location").asOpt[String]
(subsidiary, location) match {
case (None, _) => JsError(Seq(JsPath() \ "subsidiary" -> Seq(ValidationError("error.path.missing"))))
case (Some("Autre"), None) => JsError(Seq(JsPath() \ "location" -> Seq(locationError)))
case (Some("Autre"), loc @ Some(_)) => JsSuccess(("Autre", loc))
case (Some(sub), loc @ Some(_)) => JsError(Seq(JsPath() \ "location" -> Seq(locationError)))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment