-->
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.  [ 6 posts ] 
Author Message
 Post subject: Validator: Setting paylaods dynamically in CustomValidators
PostPosted: Sun Sep 08, 2013 8:03 pm 
Newbie

Joined: Tue May 07, 2013 12:00 am
Posts: 8
Location: The Middle of Middle Earth!
Hello,
Is there any way I can set payloads dynamically in a custom constraint validator?
Code:
public class DateQualifierConstraintValidator implements ConstraintValidator<DateQualifierConstraint, Object> {
   public boolean isValid(Object item, ConstraintValidatorContext context) {
      if(item fails validation1) {
         context.buildConstraintViolationWithTemplate(message1).addNode(node).addConstraintViolation();
         //I want to be able to set a payload here say Payload1.class
      }
      
      if(item fails validation2) {
         context.buildConstraintViolationWithTemplate(message2).addNode(node).addConstraintViolation();
         //I want to be able to set a payload here say Payload2.class
      }
   
   }
}


Thanks
Mark


Top
 Profile  
 
 Post subject: Re: Validator: Setting paylaods dynamically in CustomValidators
PostPosted: Mon Sep 09, 2013 4:55 am 
Hibernate Team
Hibernate Team

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

no, there is no way to set a constraint's payload from within a validator as constraints are immutable once declared.

What is the problem you want to address by setting the payload on a per-validation basis? If you give some more details on the background, we might find another solution. You also might be interested in the programmatic API for constraint declaration in HV (see http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-programmatic-api). This allows you to set constraint payloads in a programmatic manner, but this would be once per constraint and not on a per validation basis.

--Gunnar

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


Top
 Profile  
 
 Post subject: Re: Validator: Setting paylaods dynamically in CustomValidators
PostPosted: Mon Sep 09, 2013 5:56 pm 
Newbie

Joined: Tue May 07, 2013 12:00 am
Posts: 8
Location: The Middle of Middle Earth!
Thanks a lot for that, I really appreciate it. This is what I'm trying to do:

I'm trying to create a report that displays the validation errors and the number of times the validation errors occured.
For e.g. for the below xml
Code:
TitleIsMandatory             - ErrorCount(2)
CostShouldBeGreaterThan3      - ErrorCount(1) 

Code:
<itemList>
   <item>
      <title></title>
      <cost>1.1</cost>
   </item>
   <item>
      <title></title>
      <cost>3.2</cost>
   </item>
   <item>
      <title>someTitle</title>
      <cost>3.5</cost>
   </item>
</itemList>

The way I go about it is...

I add a payload to each constraint.
Code:
@NotNull(message = TITLE_EMPTY, groups = XmlAndExcelValidation.class, payload = { TitleIsMandatory.class })
public String getTitle() {
   return title;
}


I then consolidate all the errors and group the errors by payload. This works fine for all the built in constraints. It would have worked fine even for my custom constraints/validators, but then I'm doing multiple validations inside some of my custom validators. Of course, it would have been ideal if I had done just one validation per constraint, but then in some cases, it made sense to group the validations together under the same constraint. I guess, since I cannot specify the payload dynamically in my validator, the best way to go about it is to refactor my custom validators so that each validator does only one validation. That will be quite a bit of work, I'm hoping there's a better way to do this :-) Thanks.

Cheers
Mark


Top
 Profile  
 
 Post subject: Re: Validator: Setting paylaods dynamically in CustomValidators
PostPosted: Tue Sep 10, 2013 3:38 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Maybe you could use another group criteria? E.g. the message (in case it is unique) or a compound criteria object comprising the type of the constraint and its location as obtained from ConstraintViolation#getConstraintDescriptor()?

But in the latter case you'd also have to de-compose your constraints into a dedicated constraint per type of error you want to report.

Hth,

--Gunnar

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


Top
 Profile  
 
 Post subject: Re: Validator: Setting paylaods dynamically in CustomValidators
PostPosted: Wed Sep 11, 2013 6:16 pm 
Newbie

Joined: Tue May 07, 2013 12:00 am
Posts: 8
Location: The Middle of Middle Earth!
Thanks buddy. I'm going for one error per constraint.

I don't know how hard it is to do, I haven't looked at the source myself, I thought it would be a nice feature to be able to add payloads dynamically from the constraint validator.

Cheers
Mark


Top
 Profile  
 
 Post subject: Re: Validator: Setting paylaods dynamically in CustomValidators
PostPosted: Thu Sep 12, 2013 6:29 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
The issue is that constraints are read-only once configured (either via annotations, XML or the API). So there is no way to change them at validation time. If that was possible, concurrency issues might arise as each constraint instance exists exactly once; So changing its payload during one validation call might affect other validations of that constraint either going on in parallel or later on.

--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.  [ 6 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.