Hi Everyone,
I'm working on adding file upload to a form page that I have. I implemented it almost verbatim from the example
here. Here's the specific code from the example:
def upload = Action(parse.multipartFormData) { request =>
request.body.file("picture").map { picture =>
import java.io.File
val filename = picture.filename
val contentType = picture.contentType
picture.ref.moveTo(new File("/tmp/picture"))
Ok("File uploaded")
}.getOrElse {
Redirect(routes.Application.index).flashing(
"error" -> "Missing file"
)
}
}
Works great, but now I want to improve the error handling. Mainly, I do not want to redirect to somewhere else, I would like to stay on the form that I'm already on, and show a validation error. How do I do that? I tried by changing this line:
Redirect(routes.Application.index).flashing(
"error" -> "Missing file"
to this instead...
BadRequest(views.html.user.edit(myForm.withError(new FormError("photo", "My Error Message!!!"))
and while it does keep me on the form, it never displays my error message. My guess is that this is happening because "photo," the name of the file input tag, is not a field explicitly mapped in "myForm." Can anyone help me get my error message to display? Alternatively, are there any good examples out there or robust validation on forms with file uploads in Scala that I can look at? My controller is handling several different kinds of validation errors (ex: file size too big, invalid dimensions, etc...). I just need to be able to display them.
Thanks in advance!
Joe