[2.6-Scala] Json Writes for scala form with errors


(Decapo01) #1

When validating a form with

form.bindFromRequest.fold(
  formWithErrors => {
    BadRequest(Json.toJson(formWithErrors)(/*implicit writes goes here*/))
  },
  data => {}
)

is there a standard Json Writes for the form with errors?


(Decapo01) #2

Ha, stupid me I just needed to define it

implicit val myFormDataWrites = Json.writes[MyFormData]

implicit val myFormDataFormWrites = Json.writes[Form[MyFormData]]

(Decapo01) #3

Stupid me once again. Intellij wasn’t showing my compile errors for this. You don’t need to write a Json writes macro for form errors You can just use

formWithErrors.errorsAsJson

So my original code should look like this

form.bindFromRequest.fold(
  formWithErrors => {
    BadRequest(formWithErrors.errorsAsJson)
  },
  data => {
    /*do stuff with valid data*/
  }
)