How to check MultipartFile using HibernateValidator?

I have a field of type MultipartFile in a bean base that is attached 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> 

Bean Support,

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

I tried applying annotations like @NotEmpty , but didn't work. They ended the exception.

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

I am using Validation-API 1.0.0. Is it possible to check if the user does not download the file and does not press the submit button using the HibernateValidator ?

+4
source share
2 answers

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

+3
source

The restriction on the bean method of checking the 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()); } } 
+2
source

All Articles