Results 1 to 3 of 3

Thread: How do I display validation errors about an uploaded multipart file using Spring MVC

  1. #1
    Join Date
    Nov 2007
    Posts
    177

    Default How do I display validation errors about an uploaded multipart file using Spring MVC

    Hello,

    I have a Spring MVC application with a file upload form.

    I would like to be able to display validation errors to a user if the uploaded content is not valid (e.g. the content is not an image, etc.)

    However, by definition what is posted is of type: MultipartFile (multipart/form-data) and therefore I can't have a @ModelAttribute in my form and in order to use a BindingResult, it seems I do need a @ModelAttribute just before the BindingResult.

    My question is then, what is the most appropriate way of displaying validation errors to a user when all I have is a MultipartFile? I could of course manually add model attributes to the model but I am sure there is a better way.

    Thanks in advance,

    J.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Not sure what you want to validate on a file-upload?! If you want to validate the file content I doubt this is the best way to do it. But if you really must...

    Make the MultipartFile part of a model object (create a wrapper with a MultipartFile field) and validate that object.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Nov 2007
    Posts
    177

    Default

    Hi Marten,
    Thank you. It works.
    Regards,
    Julien.
    Last edited by balteo; Oct 26th, 2012 at 05:32 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •