Interface HibernateConstraintValidatorContext

All Superinterfaces:
ConstraintValidatorContext
All Known Subinterfaces:
HibernateCrossParameterConstraintValidatorContext

public interface HibernateConstraintValidatorContext extends ConstraintValidatorContext
A custom ConstraintValidatorContext which allows to set additional message parameters for interpolation.
Author:
Hardy Ferentschik, Gunnar Morling, Guillaume Smet
  • Method Details

    • buildConstraintViolationWithTemplate

      HibernateConstraintViolationBuilder buildConstraintViolationWithTemplate(String messageTemplate)
      Specified by:
      buildConstraintViolationWithTemplate in interface ConstraintValidatorContext
    • addMessageParameter

      HibernateConstraintValidatorContext addMessageParameter(String name, Object value)
      Allows to set an additional named parameter which can be interpolated in the constraint violation message. The variable will be available for interpolation for all constraint violations generated for this constraint. This includes the default one as well as all violations created by the ConstraintValidatorContext.ConstraintViolationBuilder.

      Message parameters added via this method are subject to the default parameter escaping configured for the current validation factory (see MessageParameterEscaping and BaseHibernateValidatorConfiguration.MESSAGE_PARAMETER_ESCAPING).

      To create multiple constraint violations with different variable values, this method can be called between successive calls to ConstraintValidatorContext.ConstraintViolationBuilder.addConstraintViolation().

      For example:

       
       public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
           HibernateConstraintValidatorContext context = constraintValidatorContext.unwrap( HibernateConstraintValidatorContext.class );
      
           context.addMessageParameter( "foo", "bar" );
           context.buildConstraintViolationWithTemplate( "{foo}" )
                  .addConstraintViolation();
      
           context.addMessageParameter( "foo", "snafu" );
           context.buildConstraintViolationWithTemplate( "{foo}" )
                  .addConstraintViolation();
      
           return false;
        }
        
      
       
      Parameters:
      name - the name under which to bind the parameter, cannot be null
      value - the value to be bound to the specified name
      Returns:
      a reference to itself to allow method chaining
      Throws:
      IllegalArgumentException - in case the provided name is null
      Since:
      5.4.1
    • addMessageParameter

      Allows to set an additional named parameter with explicit control over escaping behavior.

      Same as addMessageParameter(String, Object), but allows explicitly specifying the message parameter escaping option.

      When MessageParameterEscaping.ESCAPE is specified, non-Number values are escaped to prevent injection into message interpolation expressions. When MessageParameterEscaping.NONE is specified, the value is stored as-is.

      Parameters:
      name - the name under which to bind the parameter, cannot be null
      value - the value to be bound to the specified name
      escaping - the escaping behavior to use for this parameter
      Returns:
      a reference to itself to allow method chaining
      Throws:
      IllegalArgumentException - in case the provided name is null
      Since:
      9.0.2
    • addExpressionVariable

      HibernateConstraintValidatorContext addExpressionVariable(String name, Object value)
      Allows to set an additional expression variable which will be available as an EL variable during interpolation. The variable will be available for interpolation for all constraint violations generated for this constraint. This includes the default one as well as all violations created by the ConstraintValidatorContext.ConstraintViolationBuilder. To create multiple constraint violations with different variable values, this method can be called between successive calls to ConstraintValidatorContext.ConstraintViolationBuilder.addConstraintViolation().

      For example:

       
       public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
           HibernateConstraintValidatorContext context = constraintValidatorContext.unwrap( HibernateConstraintValidatorContext.class );
      
           context.addExpressionVariable( "foo", "bar" );
           context.buildConstraintViolationWithTemplate( "${foo}" )
                  .addConstraintViolation();
      
           context.addExpressionVariable( "foo", "snafu" );
           context.buildConstraintViolationWithTemplate( "${foo}" )
                  .addConstraintViolation();
      
           return false;
        }
        
      
       
      Parameters:
      name - the name under which to bind the expression variable, cannot be null
      value - the value to be bound to the specified name
      Returns:
      a reference to itself to allow method chaining
      Throws:
      IllegalArgumentException - in case the provided name is null
    • withDynamicPayload

      HibernateConstraintValidatorContext withDynamicPayload(Object payload)
      Allows to set an object that may further describe the violation. The user is responsible himself to ensure that this payload is serializable in case the jakarta.validation.ConstraintViolation has to be serialized.
      Parameters:
      payload - an object representing additional information about the violation
      Returns:
      a reference to itself to allow method chaining
      Since:
      5.3
    • getConstraintValidatorPayload

      @Incubating <C> C getConstraintValidatorPayload(Class<C> type)
      Returns an instance of the specified type or null if the current constraint validator payload isn't of the given type.
      Parameters:
      type - the type of payload to retrieve
      Returns:
      an instance of the specified type or null if the current constraint validator payload isn't of the given type
      Since:
      6.0.9
      See Also: