org.hibernate.validator.constraintvalidation

## Interface HibernateConstraintValidatorContext

- All Superinterfaces:
    ConstraintValidatorContext
  All Known Implementing Classes:
    ConstraintValidatorContextImpl

      public interface HibernateConstraintValidatorContext
      extends ConstraintValidatorContext

  A custom `ConstraintValidatorContext` which allows to set additional message parameters for interpolation.
  Author:
    Hardy Ferentschik
  Experimental
    Adding custom parameters to the context is considered experimental, especially the exact semantics regarding to which generated constraint violation these parameters apply might change. At the moment they apply to all violations.

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from interface javax.validation.ConstraintValidatorContext

      `ConstraintValidatorContext.ConstraintViolationBuilder`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `HibernateConstraintValidatorContext` | `addExpressionVariable(String name, Object value)` Allows to set an additional named variable which can be interpolated in the constraint violation message.

    - ### Methods inherited from interface javax.validation.ConstraintValidatorContext

      `buildConstraintViolationWithTemplate, disableDefaultConstraintViolation, getDefaultConstraintMessageTemplate, unwrap`

- - ### Method Detail

    - #### addExpressionVariable

          HibernateConstraintValidatorContext addExpressionVariable(String name,
                                                                  Object value)

      Allows to set an additional named variable 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 `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 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`

Copyright © 2007-2014 Red Hat, Inc. All Rights Reserved