javax.validation
Interface ConstraintValidatorContext


public interface ConstraintValidatorContext

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

Author:
Emmanuel Bernard

Nested Class Summary
static interface ConstraintValidatorContext.ConstraintViolationBuilder
          ConstraintViolation builder allowing to optionally associate the violation report to a sub path.
 
Method Summary
 ConstraintValidatorContext.ConstraintViolationBuilder buildConstraintViolationWithTemplate(java.lang.String messageTemplate)
          Return an constraint violation builder building an violation report allowing to optionally associate it to a sub path.
 void disableDefaultConstraintViolation()
          Disable the default ConstraintViolation object generation (which is using the message template declared on the constraint).
 java.lang.String getDefaultConstraintMessageTemplate()
           
 

Method Detail

disableDefaultConstraintViolation

void disableDefaultConstraintViolation()
Disable the default ConstraintViolation object generation (which is using the message template declared on the constraint). Useful to set a different violation message or generate a ConstraintViolation based on a different property.


getDefaultConstraintMessageTemplate

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

buildConstraintViolationWithTemplate

ConstraintValidatorContext.ConstraintViolationBuilder buildConstraintViolationWithTemplate(java.lang.String messageTemplate)
Return an constraint violation builder building an violation report allowing to optionally associate it to a sub path. The violation message will be interpolated.

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

If isValid returns false, a ConstraintViolation object will be built per ConstraintViolation report including the default one (unless disableDefaultConstraintViolation() 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 ConstraintViolation, a new constraint violation builder has to be retrieved from ConstraintValidatorContext Here are a few usage examples:

 // create new violation report with the default path the constraint is located on
 context.buildConstraintViolationWithTemplate( "way too long" )
             .addConstraintViolation();

 // create new violation report in the "street" subnode of the default path
 //the constraint is located on
 context.buildConstraintViolationWithTemplate( "way too long" )
              .addNode( "street" )
              .addConstraintViolation();

 //create new violation report in the "addresses["home"].city.name" subnode
 //of the default path the constraint is located on
 context.buildConstraintViolationWithTemplate( "this detail is wrong" )
              .addNode( "addresses" )
              .addNode( "country" )
                  .inIterable().atKey( "home" )
              .addNode( "name" )
              .addConstraintViolation();
 
 

Parameters:
messageTemplate - new uninterpolated constraint message.
Returns:
Returns an constraint violation builder


Submit a bug or feature

Copyright © 2009-2011, Oracle Corporation and/or its affiliates. All Rights Reserved. Use is subject to license terms.

Generated on 10-February-2011 12:41