Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate validator - conditionally apply validator

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...

like image 933
Jack Nickels Avatar asked Jan 12 '12 14:01

Jack Nickels


1 Answers

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.

like image 171
Beccari Avatar answered Oct 10 '22 15:10

Beccari