The new @ScriptAssert validator by Hibernate can be really useful for throwing together a quick validation rule that spans over several bean properties, but one of the short comings is that you can't specify a field level error.
So here is a solution to that. You'll probably notice it doesn't extend the script assert logic, it duplicates it. That's because you can't inherit from an annotation.
Hello, could you share contents of ConstraintValidatorUtils as well?
ReplyDeleteAlready found it.. https://gist.github.com/bmchild/3790115
DeleteWhy your solution is useful is also this:
ReplyDeleteIf you specify both @ScriptAssert and some other validation annotations at bean level (say @NotNull), if @ScriptAssert evaluates to 'true' @NotNull gets ignored fully. That is, an ivalid bean gets reported as valid because @ScriptAssert was used.
Hi Brett, that's a great idea. I think it should be supported by the original @ScriptAssert, so I've filed an issue in our tracker (https://hibernate.atlassian.net/browse/HV-1201). Should be part of one of the next releases.
ReplyDeleteAwesome! Thanks, Gunnar.
Delete