-->
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.  [ 5 posts ] 
Author Message
 Post subject: How do I make ConstraintValidator<Past, TimestampParam> work
PostPosted: Wed Jul 13, 2011 9:50 pm 
Newbie

Joined: Wed Jul 13, 2011 9:40 pm
Posts: 4
Basically I want to use the javax.validation.constraints.Past annotation with with my own timestamp class.
So I wrote a ConstraintValidator:

Code:
public class PastValidator implements ConstraintValidator<Past, Timestamp>
{
   /* (non-Javadoc)
    * @see javax.validation.ConstraintValidator#initialize(java.lang.annotation.Annotation)
    */
   @Override
   public void initialize(final Past past) {  }

   /* (non-Javadoc)
    * @see javax.validation.ConstraintValidator#isValid(java.lang.Object, javax.validation.ConstraintValidatorContext)
    */
   @Override
   public boolean isValid(final Timestamp tsParam, final ConstraintValidatorContext context) {
      
      return (tsParam==null || tsParam.getTime()<System.currentTimeMillis());
   }
}


I'm trying to then validate a timestamp being passed to a method:

Code:
public void method(@Past Timestamp t) {
...
}


using AspectJ to inject the validation code. What I get is this:


Quote:
javax.validation.UnexpectedTypeException: No validator could be found for type: com.test.Timestamp
at org.hibernate.validator.engine.ConstraintTree.verifyResolveWasUnique(ConstraintTree.java:383)
at org.hibernate.validator.engine.ConstraintTree.findMatchingValidatorClass(ConstraintTree.java:364)
at org.hibernate.validator.engine.ConstraintTree.getInitializedValidator(ConstraintTree.java:313)
at org.hibernate.validator.engine.ConstraintTree.validateConstraints(ConstraintTree.java:144)
at org.hibernate.validator.engine.ConstraintTree.validateConstraints(ConstraintTree.java:117)
at org.hibernate.validator.metadata.MetaConstraint.validateConstraint(MetaConstraint.java:84)
at org.hibernate.validator.engine.ValidatorImpl.validateParameterForGroup(ValidatorImpl.java:1001)
at org.hibernate.validator.engine.ValidatorImpl.validateParametersForGroup(ValidatorImpl.java:934)
at org.hibernate.validator.engine.ValidatorImpl.validateParametersInContext(ValidatorImpl.java:864)
at org.hibernate.validator.engine.ValidatorImpl.validateAllParameters(ValidatorImpl.java:225)


Looking at the validators that hibernate uses internally, I can't see any difference to how they support Date and Calendar. Does anyone have any ideas on how to make this work? Do I have to define my own Annotation for Past for my custom class?

FYI, I'm using:

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>

In my project.

thanks,

matt


Top
 Profile  
 
 Post subject: Re: How do I make ConstraintValidator<Past, TimestampParam> work
PostPosted: Thu Jul 14, 2011 4:45 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

if you want to provide a new ConstraintValidator for an existing constraint you need to register it via xml (see also http://docs.jboss.org/hibernate/stable/ ... e/#d0e2143 or the BV spec). Basically you need to xml files

1. validation.xml (in META-INF/validation.xml) to enable xml configuration
Code:
<validation-config xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration">
    <constraint-mapping>path/to/my/custom/xml/custom-constraints.xml</constraint-mapping>
</validation-config>


2. custom-constraints.xml (or whatever you want to call it)
Code:
<?xml version="1.0" encoding="UTF-8"?>
<constraint-mappings xmlns="http://jboss.org/xml/ns/javax/validation/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping">
<constraint-mappings>
    <constraint-definition annotation="javax.validation.constraints.Past">
        <validated-by include-existing-validators="false">
            <value>foo.bar.PastValidator</value>
        </validated-by>
    </constraint-definition>
</constraint-mappings>


Hope this helps.

--Hardy


Top
 Profile  
 
 Post subject: Re: How do I make ConstraintValidator<Past, TimestampParam> work
PostPosted: Thu Jul 14, 2011 10:59 am 
Newbie

Joined: Wed Jul 13, 2011 9:40 pm
Posts: 4
Thanks, I'll give that a shot... I can't tell you how long I was searching google for the right set of key words for this :)


Top
 Profile  
 
 Post subject: Re: How do I make ConstraintValidator<Past, TimestampParam> work
PostPosted: Thu Jul 14, 2011 2:05 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi,

I've described the required steps for adding validators for the @Past/@Future constraints also in this blog post I wrote a while ago. Note that the post is about adding support for the Joda time API, in between these types are supported out of the box by HV 4.2.

Hth,

--Gunnar

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


Top
 Profile  
 
 Post subject: Re: How do I make ConstraintValidator<Past, TimestampParam> work
PostPosted: Thu Jul 14, 2011 3:08 pm 
Newbie

Joined: Wed Jul 13, 2011 9:40 pm
Posts: 4
Thanks. I finally got around to trying this out and it works great!


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