Friday, June 7, 2013

Use @ScriptAssert logic With a Field Error

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.

5 comments:

  1. Hello, could you share contents of ConstraintValidatorUtils as well?

    ReplyDelete
    Replies
    1. Already found it.. https://gist.github.com/bmchild/3790115

      Delete
  2. Why your solution is useful is also this:

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

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

    ReplyDelete