-->
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.  [ 4 posts ] 
Author Message
 Post subject: RangeValidator Question (there in 3.1.0.GA, not 4.0.2.GA)
PostPosted: Sat Apr 24, 2010 1:29 pm 
Newbie

Joined: Fri Apr 09, 2010 3:55 pm
Posts: 8
Hi,

We are using Hibernate Validator to augment the UI code that constructs a form (using Vaadin as the UI framework). We are scanning our beans looking for validation annotations, then marking the field (constructed by Vaadin) with a special "do validation this way" method.

we have something like this:
Code:
annotation = attribute.getAnnotation(Range.class);
if (annotation != null) {
    final Range rangeAnnotation = (Range)annotation;
    field.addValidator(new RangeValidator(getI18nValidationMessage(rangeAnnotation.message()), rangeAnnotation));
}


Then, in our RangeValidator, we have something that *should* be like this:

Code:
public RangeValidator(final String message, final Range rangeAnnotation) {
    super(message);
    rangeValidator = new org.hibernate.validator.constraints.impl.RangeValidator();
    rangeValidator.initialize(rangeAnnotation);
}


I say *should* because org.hibernate.validator.constraints.impl.RangeValidator does not exist, whereas another class that we *are* using, org.hibernate.validator.constraints.impl.EmailValidator does exist.

All this is okay (sort of :-)), I can easy write my own RangeValidator like the Reference documentation says, but I have a few questions:

1. Should org.hibernate.validator.constraints.impl.RangeValidator not be there? (it's in Validator 3.1.0.GA)
2. If it has been removed, what replaces it? Would @Size offer the equivalent functionality?

Thank you.

-=bootlaces=-


Top
 Profile  
 
 Post subject: Re: RangeValidator Question (there in 3.1.0.GA, not 4.0.2.GA)
PostPosted: Sat Apr 24, 2010 2:55 pm 
Newbie

Joined: Fri Apr 09, 2010 3:55 pm
Posts: 8
Hi,

For those interested (and whilst I'm still waiting an answer :-), this may be of help:

Code:
@SuppressWarnings("serial")
public class RangeValidator extends AbstractStringValidator {

    private final MinValidatorForString minValidator;
    private final MaxValidatorForString maxValidator;

    public RangeValidator(final String message, final Range rangeAnnotation) {
        super(message);

        minValidator = new MinValidatorForString();
        maxValidator = new MaxValidatorForString();

        final ClassLoader classLoader = getClass().getClassLoader();

        final Min min = (Min)Proxy.newProxyInstance(classLoader, new Class<?>[]{Min.class}, new InvocationHandler() {

            public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
                return rangeAnnotation.min();
            }
        });

        final Max max = (Max)Proxy.newProxyInstance(classLoader, new Class<?>[]{Max.class}, new InvocationHandler() {

            public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
                return rangeAnnotation.max();
            }
        });

        minValidator.initialize(min);
        maxValidator.initialize(max);

    }

    @Override
    protected boolean isValidString(final String value) {
        return minValidator.isValid(value, null) && maxValidator.isValid(value, null);
    }
}


AbstractStringValidator is a Vaadin class, the system calls isValidString.

-=bootlaces=-


Top
 Profile  
 
 Post subject: Re: RangeValidator Question (there in 3.1.0.GA, not 4.0.2.GA)
PostPosted: Sun Apr 25, 2010 6:14 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
As of Hibernate Validator 4 @Range is realized as so-called composed constrained (based on @Min and @Max), therefore there is no need for a dedicated validator anymore. Btw. I would suggest to work with @Size, as it is the standard annotation from the JSR 303 API.

I don't know Vaadin, but your approach for integrating Bean Validation/Hibernate Validator seems a bit unusual to me. Did you take a look at javax.validation.Validator#validateValue() already? This method checks, whether a given value would be valid for a given attribute of a class, without the need to actually write this value into the model. This allows for a more generic integration of the BV API, and also is the approach followed by JSF 2.

Gunnar

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


Top
 Profile  
 
 Post subject: Re: RangeValidator Question (there in 3.1.0.GA, not 4.0.2.GA)
PostPosted: Sun Apr 25, 2010 1:21 pm 
Newbie

Joined: Fri Apr 09, 2010 3:55 pm
Posts: 8
Hi Gunnar,

Thank you for your helpful reply :-)

-=bootlaces=-


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