[2.1.1 Scala] File upload with validation of said file

241 views
Skip to first unread message

Joe Zulli

unread,
May 30, 2013, 8:10:32 PM5/30/13
to play-fr...@googlegroups.com
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 


Guillaume Bort

unread,
May 31, 2013, 10:08:48 AM5/31/13
to play-fr...@googlegroups.com
How do you try to retrieve the error message in the template?


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Guillaume Bort, http://guillaume.bort.fr

Joe Zulli

unread,
May 31, 2013, 2:21:15 PM5/31/13
to play-fr...@googlegroups.com
Hi Guillaume,

Thanks for the hint :). I was indeed not retrieving the error message correctly in the template. Here's what I ended up doing that worked:

            <div class="control-group controls-row @form.errors.filter(_.key == "photo").map( error => "error")">
                <label for="photo" class="control-label">Photo</label>
                <img src="@asset("images/no_image_80_x_50.png")" class="img-polaroid">
                <input type="file" id="photo" name="photo">
                @chainForm.errors.filter(_.key == "photo").map { error =>
                    <p class="help-block">@error.message</p>
                }
            </div>

Hope that helps for others who are trying to do the same thing. It's a little funny looking, but it works :). If anyone has a better or more elegant way to do it, let me know. I'm still new-ish to Play.

Joe
Reply all
Reply to author
Forward
0 new messages