Search code examples
hibernatejakarta-eebean-validation

custom constraints with non constant parameter


I would like to create a new constraint to validate an input but it need to know the value of an other input of the same form. I saw that custom constraints created with the @interface annotation only accept constant expression as parameter. is there any other way to do it ?


Solution

  • You can either write a class level constraint or you use the mentioned @ScriptAssert. In the former you place the constraint on class level and you get the whole instance passed to isValid. You can then inspect any values you like and do your checks.

    The alternative is to use the Hibernate Validator provided @ScriptAssert (again on class level so that the whole instance gets bound into the script context). You can then write your checks in your scripting language of choice, eg JavaScript. Of course the scripting engine must be available on your runtime environment.