4

I have a field of type MultipartFile in a backing bean which is bound to a Spring form (I'm using MultipartFilter),

<form:form htmlEscape="false" action="${pageContext.request.contextPath}/admin_side/Category.htm" id="dataForm" name="dataForm" method="post" commandName="categoryBean" enctype="multipart/form-data">

    <input type="file" id="txtCatImage" name="txtCatImage"/>

</form:form>

Backing Bean,

final public class CategoryBean
{
     private MultipartFile txtCatImage=null;

     public MultipartFile getTxtCatImage()
     {
          return txtCatImage;
     }

     public void setTxtCatImage(MultipartFile txtCatImage)
     {
         this.txtCatImage = txtCatImage;
     }
}

I have tried to apply annotations like @NotEmpty but didn't work. They ended up with an exception.

javax.validation.UnexpectedTypeException: No validator could be found for type: org.springframework.web.multipart.MultipartFile

I'm using Validation-API 1.0.0. Is this possible to perform a validation, if a user doesn't upload a file and press a submit button using HibernateValidator?

Tiny
  • 27,221
  • 105
  • 339
  • 599
  • Someone seems to have ask this same question before. Check this [link](http://stackoverflow.com/questions/7161215/spring-multipartfile-validation-and-conversion) – Angel Villalain Dec 05 '12 at 03:52
  • @Angel Villalain - Thanks for the link. But that question is about [org.springframework.validation.Validator](http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/validation/Validator.html). I used to perform validation in that way with old-fashioned controllers like [SimpleFormController](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/mvc/SimpleFormController.html). I'm using annotated controllers with HibernateValidator which that approach will not work with. – Tiny Dec 05 '12 at 03:56

2 Answers2

5

Now I understand what you are trying to accomplish specifically. Well you could implement your own custom validation. For example, instead of implementing the Spring validator interface, implement a Hibernate ConstraintValidator, for a specific annotation that you define for this specific case. Check this link. In this case you could add an implementation for a @NotNull validator for a MultipartFile object.

Angel Villalain
  • 595
  • 4
  • 16
  • Until now I have been familiar with [cross field validation](http://stackoverflow.com/q/11890334/1391249) but not with this. Done according to the link in your answer. Thank you. – Tiny Dec 06 '12 at 18:32
  • Cross field is something I still have to try, I think that was added recently to hibernate validator. – Angel Villalain Dec 07 '12 at 04:09
3

Putting constraint on bean method checking file on emptiness worked for me:

final public class CategoryBean
{
  private MultipartFile txtCatImage = null;
  public MultipartFile getTxtCatImage() { return txtCatImage; }
  public void setTxtCatImage(MultipartFile txtCatImage) { this.txtCatImage = txtCatImage; }

  @AssertTrue(message = "File must be provided")
  public boolean isFileProvided() {
    return (txtCatImage != null) && ( ! txtCatImage.isEmpty());
  }
}
Gedrox
  • 3,592
  • 1
  • 21
  • 29