javax.validation
Interface ConstraintValidatorContext


public interface ConstraintValidatorContext

Provide contextual data and operation when applying a given constraint validator. At least one error must be defined (either the default one, of if the default error is disabled, a custom one).

Author:
Emmanuel Bernard

Nested Class Summary
static interface ConstraintValidatorContext.ErrorBuilder
          Error builder allowing to optionally associate the error to a sub path.
 
Method Summary
 ConstraintValidatorContext.ErrorBuilder buildErrorWithMessageTemplate(java.lang.String messageTemplate)
          Return an error builder building an error allowing to optionally associate the error to a sub path.
 void disableDefaultError()
          Disable the default error message and default ConstraintViolation object generation.
 java.lang.String getDefaultErrorMessageTemplate()
           
 

Method Detail

disableDefaultError

void disableDefaultError()
Disable the default error message and default ConstraintViolation object generation. Useful to set a different error message or generate a ConstraintViolation based on a different property


getDefaultErrorMessageTemplate

java.lang.String getDefaultErrorMessageTemplate()
Returns:
the current uninterpolated default message

buildErrorWithMessageTemplate

ConstraintValidatorContext.ErrorBuilder buildErrorWithMessageTemplate(java.lang.String messageTemplate)
Return an error builder building an error allowing to optionally associate the error to a sub path. The error message will be interpolated.

To create the error, one must call either one of the #addError method available in one of the interfaces of the fluent API. If another method is called after #addError() on ErrorBuilder or any of its associated nested interfaces an IllegalStateException is raised.

If isValid returns false, a ConstraintViolation object will be built per error including the default one unless disableDefaultError() has been called.

ConstraintViolation objects generated from such a call contain the same contextual information (root bean, path and so on) unless the path has been overriden.

To create a different error, a new error builder has to be retrieved from ConstraintValidatorContext Here are a few usage examples:

 // create new error with the default path the constraint is located on
 context.buildErrorWithMessageTemplate( "way too long" )
             .addError();

 // create new error in the "street" subnode of the default path the constraint
 // is located on
 context.buildErrorWithMessageTemplate( "way too long" )
              .addSubNode( "street" )
              .addError();

 //create new error in the "addresses["home"].city.name subnode of the default
 // path the constraint is located on
 context.buildErrorWithMessageTemplate( "this detail is wrong" )
              .addSubNode( "addresses" )
              .addSubNode( "country" )
                  .inIterable().atKey( "home" )
              .addSubNode( "name" )
              .addError();
 
 

Parameters:
messageTemplate - new uninterpolated error message.
Returns:
Returns an error builder


Copyright © 2007-2009. All Rights Reserved.