Java – JSF/Hibernate NotBlank validation

bean-validationjavajsfrichfacesvalidation

I have a simple JSF+RichFaces form with some fields and obviously a backing bean to store them. In that bean all the necessary properties have validation annotations (jsr303/hibernate), but I can't seem to find an annotation which would check if the property (String) is blank. I know there's a @NotBlank annotation in spring modules, but JSF doesn't support spring validation. Is there any easy way to check it or should I write my own annotation?

@Edit: I already tried @NotNull and @NotEmpty from jsr303 and hibernate, but they both failed I still can send a blank string like " ".

Best Answer

If you use Hibernate Validator 4.1 as your JSR-303 implementation, they provide a @NotBlank annotation that does EXACTLY what you're looking for, separate from @NotNull and @NotEmpty. You need to be using the (currently) latest version, but that will work.

If you can't go to the latest version for some reason, it doesn't take much to write an annotation yourself.