Hibernate validator – conditionally apply validator

hibernate-validatorvalidation

I want to apply certain @NotBlank annotations only if another field is false. On my current project we have to use hibernate validator, so no commons validation + valang available to solve this.

Since I'm not too fond of creating multiple custom annotations just to solve what should be an easy thing (bit bummed that this isn't part of the JSR-303 spec) I was wondering if anyone knew an acceptable way to accomplish this. I found a jar which accomplished that, but that was for hibernate 3. And due to the changes…

Best Answer

For this case, the suggested approach is to use class-level constraints instead Field or Property Level.

Please refer Hibernate Validator Docs

I now it is annoying. I had a scenario like this and tried something like @NotEmpty(depends="anotherField") and it was a totally failure.

Maybe some other members know another way to do this, but for now, I'm doing what Hibernate Validator says to do.

Related Topic