Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Add message parameters in isValid
PostPosted: Wed Oct 06, 2010 10:08 am 
Newbie

Joined: Wed Oct 06, 2010 9:27 am
Posts: 1
Location: Fort Knox, KY
Is there a way to add message parameters using ConstraintValidatorContext?

In the below example I want to add 3 values to the ConstraintValidatorContext:

ValidationMessages.properties:
Code:
validator.year={0} is outside the acceptable range. Value must be between {1} and {2}.


The isValid method from my custom validator:
Code:
public boolean isValid(Long year, ConstraintValidatorContext context) {

    boolean result = true;

    if ( year == null ) {
        return result;
    }

    int currentYear = Calendar.getInstance().get(Calendar.YEAR);

    if ( year > currentYear + 1 || year < 1900 ) {

        // disable the default constraint violation
        context.disableDefaultConstraintViolation();
       
        // I want to add message parameters here but I'm not sure how to do that.

        // my own custom error message
        context.buildConstraintViolationWithTemplate("{validator.year}").addConstraintViolation();

        result = false;
    }       

    return result;
}


Top
 Profile  
 
 Post subject: Re: Add message parameters in isValid
PostPosted: Tue Mar 08, 2011 5:06 pm 
Newbie

Joined: Tue Mar 08, 2011 5:01 pm
Posts: 1
Hi,

I have the same kind of need.
Looking at the specification document, I saw that parameters have to be written in the annotation declaration or they won't be resolve at display time. Is there a workaround to be able to inject this information at runtime ?

Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.