-->
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.  [ 10 posts ] 
Author Message
 Post subject: How to report a class constraint to a field?
PostPosted: Wed Oct 14, 2009 8:28 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
I have read the most recent documentation and I was curious how to tell Hibernate Validator to return the error on a field even though it is a class constraint. Take this for example:

Code:
@UniqueResumeSkill( message = "{resumeSkill.unique}" )
public class ResumeSkill extends DomainObject {

   /* Fields */
   @NotNull( message = "{resumeSkill.resume.notNull}" )
   private Resume resume;

   @NotNull( message = "{resumeSkill.skill.notNull}" )
   private Skill skill;
   private String years;
....
}

I'd really love it if that @UniqueResumeSkill constraint could actually report as an error on the "skill" field/property. I really have no clue how to tell Hibernate Validator to do that. I thought other people may be curious how to do this too, as this is a pretty common problem that happens frequently.

The reason is that Spring really depends on bean property paths, and while with a bit of effort it is possible to dig for the class-level constraint in the Spring errors object, I'd rather just point it to the skill field/property. Semantically, that's really where the error is in this case. They need would need to select a different Skill from the drop down box, so it makes sense to put the error here than at the class/form level.

Does Hibernate Validator have this capability yet? Do you think it may in the future if it doesn't already? Any notable workarounds? :)

Thanks!


Top
 Profile  
 
 Post subject: Re: How to report a class constraint to a field?
PostPosted: Wed Oct 14, 2009 8:57 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
I looked into ConstraintValidatorContext and ConstraintValidatorContextImpl, but I don't see the obvious answer here. Most of these methods are getters, and the one buildBlah( string ) method returns an exception saying it's abstract ;)


Top
 Profile  
 
 Post subject: Re: How to report a class constraint to a field?
PostPosted: Wed Oct 14, 2009 1:15 pm 
Hibernate Team
Hibernate Team

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

you probably want to do something like this in the validator once you figured out that the problem is in the skill field. Or course you can also provide a different message template:

Code:
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate( "{resumeSkill.skill.notNull}" )
    .addNode( "skill" )
    .addConstraintViolation();


Have a look at ConstraintValidatorContextTest (for example in fisheye)

--Hardy


Top
 Profile  
 
 Post subject: Re: How to report a class constraint to a field?
PostPosted: Wed Oct 14, 2009 6:29 pm 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
I tried the following bit of code, but I get a compile-time error:

Code:
      ConstraintValidatorContextImpl impl = ( ConstraintValidatorContextImpl ) context;
      if( impl != null ) {
         impl.disableDefaultConstraintViolation();
         impl.buildConstraintViolationWithTemplate( messageTemplate )
            .addNode( property )
            .addConstraintViolation();
      }


The error:
Code:
addNode(java.lang.String) in javax.validation.ConstraintValidatorContextImpl.ConstraintViolationBuilder is defined in an inaccessible class or interface.


:(


Top
 Profile  
 
 Post subject: Re: How to report a class constraint to a field?
PostPosted: Wed Oct 14, 2009 6:48 pm 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
perhaps it would be best to wrap this into a rerouteErrorToProperty() ;)


Top
 Profile  
 
 Post subject: Re: How to report a class constraint to a field?
PostPosted: Thu Oct 15, 2009 2:51 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
I am not sure why you have to cast to ConstraintValidatorContextImpl. The isValid method in the ConstraintValidator implementation gets passed an instance of ConstraintValidatorContext. Use the public API on this instance.

Have you seen the usage examples in fisheye?

--Hardy


Top
 Profile  
 
 Post subject: Re: How to report a class constraint to a field?
PostPosted: Thu Oct 15, 2009 4:05 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
The problem hardy is that these methods are not on the interface. My IDE says they are red. Could I possibly have an older copy of javax.validation while still having 4.0.0.ga of hibernate validator? I'm just letting maven do it's thing.

Regardless, casting should be safe here.. no? I don't plan on using another implementation other than Hibernate.

I'll look at the fisheye stuff. I just took your code and assumed it would be correct.


Top
 Profile  
 
 Post subject: Re: How to report a class constraint to a field?
PostPosted: Thu Oct 15, 2009 4:21 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
Well, I see that you're having an easier time compiling that group/persons example at the bottom of the file.

This is the error I get:

Code:
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure
C:\IdeaProjects\jawbs\src\main\java\myproject\validation\HibernateValidatorSupport.java:[53,4] addNode(java.lang.String) in javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder is defined in an inaccessible class or interface


So it's not just my IDE... it's maven too :(

If I don't include the Impl, this is what I get:

Code:
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure

C:\IdeaProjects\myproject\src\main\java\jawbs\validation\HibernateValidatorSupport.java:[49,9] cannot find symbol
symbol  : method disableDefaultConstraintViolation()
location: interface javax.validation.ConstraintValidatorContext

C:\IdeaProjects\ myproject\src\main\java\jawbs\validation\HibernateValidatorSupport.java:[50,9] cannot find symbol
symbol  : method buildConstraintViolationWithTemplate(java.lang.String)
location: interface javax.validation.ConstraintValidatorContext


Top
 Profile  
 
 Post subject: Re: How to report a class constraint to a field?
PostPosted: Thu Oct 15, 2009 4:31 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
Yay, I got it to work!

I added this to maven and it seems like it works:

Code:
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>


Perhaps telling your maven dependencies in 4.0.0.ga to pull this specific version is in order?

Thanks Hardy


Top
 Profile  
 
 Post subject: Re: How to report a class constraint to a field?
PostPosted: Thu Oct 15, 2009 4:58 am 
Hibernate Team
Hibernate Team

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

I think the problem lies somewhere on your side. Hibernate Validator 4.0.0.GA already has a transitive dependency to validation-api 1.0.0.GA (http://repository.jboss.com/maven2/org/ ... 0.0.GA.pom) You should not have to specify it explicitly.

Maybe you maven repo is corrupt. Might be worth just deleting your local repo and start from scratch.

Anyways, happy to hear that ConstraintValidatorContext works now for you :)

--Hardy


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