-->
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: Validator Class Level Validations
PostPosted: Fri Jul 22, 2011 9:35 am 
Newbie

Joined: Thu Aug 26, 2010 3:37 pm
Posts: 6
I created some class-level validations that are working, however the property path returned when those validations are triggered is always the same. Here is my validator:

Code:
public class PurposeCodeValidator implements ConstraintValidator<PurposeCodeNotNull, Request> {

   @Override
   public void initialize(PurposeCodeNotNull constraintAnnotation) {

   }
   
   @Override
   public boolean isValid(Request object, ConstraintValidatorContext constraintContext) {
      if (object == null) {
         return false;
      }
      
      String formType = object.getRequests().getFormType();
      String requestType = object.getNewPrdAction();
      String purposeCode = object.getPurposeCode();
      
      if (StringUtils.contains("SF.LF.CF.CM.PEF.LDF", formType) && ! StringUtils.equals(requestType, "50") &&
            StringUtils.isBlank(purposeCode)) {
         return false;
      }
      
      return true;
   }

}


I've added the @PurposeCodeNotNull annotation to the Request class. There are three Request objects in my object graph contained within an ArrayList. Each of the Request objects has a null purposeCode property based on the logic in my validator class and I see the following constraints when I do a print out:

requests.request[2]: Purpose Code is a required field.
requests.request[2]: Purpose Code is a required field.
requests.request[2]: Purpose Code is a required field.

If I remove the @PurposeCodeNotNull annotation from the Request class and add the @NotEmpty annotation to the purposeCode property within the Request class, the property path is printed correctly:

requests.request[0].purposeCode: may not be empty
requests.request[1].purposeCode: may not be empty
requests.request[2].purposeCode: may not be empty

Am I doing something wrong or missing something in my custom validator class? I am hoping to see this print out when using the class level validations:

requests.request[0]: Purpose Code is a required field.
requests.request[1]: Purpose Code is a required field.
requests.request[2]: Purpose Code is a required field.


Top
 Profile  
 
 Post subject: Re: Validator Class Level Validations
PostPosted: Fri Jul 22, 2011 9:58 am 
Newbie

Joined: Thu Aug 26, 2010 3:37 pm
Posts: 6
I just found the answer myself and wanted to post in case any one has this same issue. I added the following to the isValid method of my custom validator class:

Code:
constraintContext.disableDefaultConstraintViolation();
constraintContext.buildConstraintViolationWithTemplate("{purposecodenotnull}").addNode("purposeCode").addConstraintViolation();


After adding that code, the print out I get is:

requests.request[0].purposeCode: Purpose Code is a required field.
requests.request[1].purposeCode: Purpose Code is a required field.
requests.request[2].purposeCode: Purpose Code is a required field.

Is this normal behavior for class level validations?


Top
 Profile  
 
 Post subject: Re: Validator Class Level Validations
PostPosted: Sun Jul 24, 2011 7:09 am 
Hibernate Team
Hibernate Team

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

thanks for your report. This indeed doesn't look right, I created HV-509 for this.

--Gunnar

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


Top
 Profile  
 
 Post subject: Re: Validator Class Level Validations
PostPosted: Fri Aug 19, 2011 11:56 am 
Newbie

Joined: Tue Aug 16, 2011 5:58 pm
Posts: 6
Quote:
The cause seems to be that the same PathImpl instance is used within both violations. When the 2nd violation is created, the index of the shared PathImpl instance originally created for the first violation is set from 0 to 1.

I think this can be circumvented by creating a copy of the path in the constructor of ConstraintValidatorContextImpl.


This seems to make sense, as it is common for numbered actions to affect the desired outcome of others in the same string, if that makes sense. I know that when I was trying to conduct some cloud computing IT experiments to repair all of my Hibernate mail files that I ran in to some issues with this. I didn't realize that I would run in to this kind of brick wall as an IT professional. Maybe next time I will talk to an IT staffing agency. Hope I was a help guys!


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.