Situation: I would like to perform Hibernate Validation based upon user properties (to allow different validation rules for input based upon a user\'s account d
Within annotations you can only refer to constant expressions, so loading values from a property file or database wouldn't work here.
You could use the API for dynamic constraint declaration introduced in Hibernate Validator 4.2 which allows to define constraints at runtime. Your example might look like that:
String dynamicPattern = ...;
ConstraintMapping mapping = new ConstraintMapping();
mapping.type( ContactInfo.class )
.property( "workPhone", FIELD )
.constraint( new PatternDef().regexp( dynamicPattern ) );
HibernateValidatorConfiguration config =
Validation.byProvider( HibernateValidator.class ).configure();
config.addMapping( mapping );
Validator validator = config.buildValidatorFactory().getValidator();