-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Validations with dynamical values
PostPosted: Mon Apr 08, 2013 2:06 pm 
Newbie

Joined: Mon Apr 08, 2013 1:56 pm
Posts: 1
Hi all,

Presently we are using JSR303 for validations in my project.
The sample code is like this.
Ex:
@Range(min = 1, max = 150)
int age;


If I want to get the min value and max value from database.

Actually I am getting the values and having in some object. But don't know how to pass that values for min and max.

Regards,
Sai.


Top
 Profile  
 
 Post subject: Re: Validations with dynamical values
PostPosted: Mon Apr 08, 2013 6:42 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Java doesn't allow to set annotation attributes dynamically. Instead you could use the HV programmatic constraint API for declaring the constraint and determine its values dynamically like this:

Code:
HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();

int min = getMinValue();
int max = getMaxValue();

ConstraintMapping mapping = configuration.createConstraintMapping();
mapping.type(MyBean.class)
    .property("age", FIELD)
        .constraint(
            new RangeDef()
                .min(min)
                .max(max)
        );

configuration.addMapping(mapping);
ValidatorFactory factory = configuration.buildValidatorFactory();
Validator validator = factory.getValidator();


You can learn more in the HV reference guide (http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#programmaticapi).

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 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.