-->
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: Group usage for partial validation and ordered validation
PostPosted: Tue Mar 08, 2011 4:45 am 
Newbie

Joined: Tue Feb 02, 2010 9:00 am
Posts: 19
Hi,

I have a such bean to validate.

Code:

public class AccountBean {

   @NotEmpty(groups = OrderChecks.class, message = "{xxx.account.cc}")
   @IsBannedCard(groups = OrderChecks.class, message = "{xxx.constraints.bannedCCNumber}")
   @IsValidCardNumber(groups = OrderChecks.class, message = "{xxx.account.invalidCardNumber}")
   private String creditCart;

   @NotEmpty(groups = PersonalChecks.class, message = "{xxx.account.userId}")
   private String userId
}


In this example group are avaliable in order to partially validate bean. This part of the JSR303 is great. At initialize page I am validating user, at order page I am validating creditCard. So far so good.

But groups can be used in order to validate in a particular order with GroupSequence too.

My objection is raising at this point.

For example at this sample I would like to validate credit card in a particular order. In case a notEmtpy violation, IsBannedCard or IsValidCard Constraint should be checked. I couldnt a way to do it.

To sum up,

My requirements:

1) I would like to partially validate my object as stated at [1] .

2) I would like to a particual order in order to validate my bean and stopping validation for that field in case a violation. [2]

I believe that I think something wrong but still I could not find. Groups are used for 2 different purpose. Maybe It could better to seperate these usages with different keywords.


By the way I have a proposal too :)

ReportAsSingleViolation is very useful too. It can solve many problems. For my case it can not solve problem because of having a requirement about error messages. I would like to display error message related to constraint. However current implementation of the ReportAsSingleViolation can not delegate error messages to other composing constraints. Maybe ReportAsSingleViolation can be extended to delegate these messages to composing constraints?



Any idea about these?

Thanks



[1] http://people.redhat.com/~ebernard/vali ... api-groups

[2] http://people.redhat.com/~ebernard/vali ... upsequence


Top
 Profile  
 
 Post subject: Re: Group usage for partial validation and ordered validation
PostPosted: Tue Mar 08, 2011 7:00 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Why are you just not defining a group sequence for your credit card checks?

Code:
@GroupSequence({ OrderCheck1.class, OrderCheck2.class, OrderCheck3.class })
interface OrderChecks {
}

public class AccountBean {

   @NotEmpty(groups = OrderCheck1.class, message = "{xxx.account.cc}")
   @IsBannedCard(groups = OrderCheck2.class, message = "{xxx.constraints.bannedCCNumber}")
   @IsValidCardNumber(groups = OrderCheck2.class, message = "{xxx.account.invalidCardNumber}")
   private String creditCart;

   @NotEmpty(groups = PersonalChecks.class, message = "{xxx.account.userId}")
   private String userId
}


Quote:

By the way I have a proposal too :)

Which is?

Quote:

ReportAsSingleViolation is very useful too. It can solve many problems. For my case it can not solve problem because of having a requirement about error messages. I would like to display error message related to constraint. However current implementation of the ReportAsSingleViolation can not delegate error messages to other composing constraints. Maybe ReportAsSingleViolation can be extended to delegate these messages to composing constraints?

I am not sure I understand. What exactly do you mean with delegate. There are really only two choices here. Have a single error message from the composing constraint itself or let the sub constraints each produce their error message.

--Hardy


Top
 Profile  
 
 Post subject: Re: Group usage for partial validation and ordered validation
PostPosted: Tue Mar 08, 2011 10:47 am 
Newbie

Joined: Tue Feb 02, 2010 9:00 am
Posts: 19
Hi,


Honestly I am trying to learn validation framework and sometimes because of my poor english I have difficulties in order to express myself.

hardy.ferentschik wrote:
hy are you just not defining a group sequence for your credit card checks?


I have tried your recommandation. I have another question regarding it.

Code:
public class AccountBean {

   @NotEmpty(groups = OrderChecks.class, message = "{xxx.account.cc}")
   @IsBannedCard(groups = OrderChecks.class, message = "{xxx.constraints.bannedCCNumber}")
   @IsValidCardNumber(groups = OrderChecks.class, message = "{xxx.account.invalidCardNumber}")
   private String creditCart;

   @NotEmpty(groups = OrderChecks.class, message = "{xxx.account.ccVal}")
   private String cc;

   @NotEmpty(groups = PersonalChecks.class, message = "{xxx.account.userId}")
   private String userId
}


Can you help me to implement such a validation? Note that I have just added another field "cc".

For the proposal part (maybe proposal is not good word :) ),

I had just wanted to has a single error message for compound constraints. For my requirement neither displaying error composing constraint itself nor all messages from sub constraint are necessary for me. It is nice to have only first violated constraint message. With delegated I wanted to express it.
If a validated property is null, it should not show @Size error message too. Displaying only notNull message is what I want.

Code:
@Documented
@NotNull
@Size(min=1)
@ReportAsSingleViolation
@Constraint(validatedBy = NotEmpty.NotEmptyValidator.class)
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
public @interface NotEmpty {}
 



Another solution which came to my mind is adding a new annotation for such kind of usages.

Maybe @ValidateUntilViolation ? It is clearer than for having different many interfaces.

Code:
   @ValidateUntilViolation
   @NotEmpty(groups = OrderChecks.class, message = "{xxx.account.cc}")
   @IsBannedCard(groups = OrderChecks.class, message = "{xxx.constraints.bannedCCNumber}")
   @IsValidCardNumber(groups = OrderChecks.class, message = "{xxx.account.invalidCardNumber}")
   private String creditCart;



Maybe what I have written does not make sense. Please forgive my ignorance.

Thanks


Top
 Profile  
 
 Post subject: Re: Group usage for partial validation and ordered validation
PostPosted: Wed Mar 09, 2011 6:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
I had just wanted to has a single error message for compound constraints. For my requirement neither displaying error composing constraint itself nor all messages from sub constraint are necessary for me. It is nice to have only first violated constraint message. With delegated I wanted to express it.
If a validated property is null, it should not show @Size error message too. Displaying only notNull message is what I want.

Code:
@Documented
@NotNull
@Size(min=1)
@ReportAsSingleViolation
@Constraint(validatedBy = NotEmpty.NotEmptyValidator.class)
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
public @interface NotEmpty {}
 



Another solution which came to my mind is adding a new annotation for such kind of usages.

Maybe @ValidateUntilViolation ? It is clearer than for having different many interfaces.

Code:
   @ValidateUntilViolation
   @NotEmpty(groups = OrderChecks.class, message = "{xxx.account.cc}")
   @IsBannedCard(groups = OrderChecks.class, message = "{xxx.constraints.bannedCCNumber}")
   @IsValidCardNumber(groups = OrderChecks.class, message = "{xxx.account.invalidCardNumber}")
   private String creditCart;



Maybe what I have written does not make sense. Please forgive my ignorance.

I don't really see what you are doing in your first suggestion, but I think you are missing something essential here. Even though your annotations in your second example are nicely ordered in code (as in you are writing them in a certain order), there is no way to keep this information at runtime. When retrieving annotations via reflection the order in which you placed them in code is not available. You can just find out which annotations are placed on a field/method. So in your example there would be no guarantee of order between NotEmpty, IsBannedCard and IsValidCardNumber.

--Hardy


Top
 Profile  
 
 Post subject: Re: Group usage for partial validation and ordered validation
PostPosted: Wed Mar 09, 2011 8:50 am 
Newbie

Joined: Tue Feb 02, 2010 9:00 am
Posts: 19
hardy.ferentschik wrote:
Quote:

I don't really see what you are doing in your first suggestion, but I think you are missing something essential here. Even though your annotations in your second example are nicely ordered in code (as in you are writing them in a certain order), there is no way to keep this information at runtime. When retrieving annotations via reflection the order in which you placed them in code is not available. You can just find out which annotations are placed on a field/method. So in your example there would be no guarantee of order between NotEmpty, IsBannedCard and IsValidCardNumber.

--Hardy




I was assumming particular order in via reflection. Sorry about my ignorance.

But I think that it is still not a big problem.

What about this?

Quote:

@ValidateUntilViolation(order= {NotEmpty.class,IsBannedCard.class,IsValidCardNumber.class})
@NotEmpty(groups = OrderChecks.class, message = "{xxx.account.cc}")
@IsBannedCard(groups = OrderChecks.class, message = "{xxx.constraints.bannedCCNumber}")
@IsValidCardNumber(groups = OrderChecks.class, message = "{xxx.account.invalidCardNumber}")
private String creditCart;


And I want to make clearer my previous question. I have legacy codes which are validated by an internal project at my company. CreditCard and CC are validated at same phase (after post of credit card page). However userId is validated before this payment page.

My requirements

* Validating creditcard and cc in same phase without validating UserId (Partial validation)
* CreditCart validation must be at a particular order
* For Credit Card it should be just one error at each time. (If CreditCart is empty, it should not be validated by IsBannedCard validator.) ( GroupSequence? )


Can you help me about it?

Code:
public class AccountBean {

   @NotEmpty(groups = OrderChecks.class, message = "{xxx.account.cc}")
   @IsBannedCard(groups = OrderChecks.class, message = "{xxx.constraints.bannedCCNumber}")
   @IsValidCardNumber(groups = OrderChecks.class, message = "{xxx.account.invalidCardNumber}")
   private String creditCart;

   @NotEmpty(groups = OrderChecks.class, message = "{xxx.account.ccVal}")
   private String cc;

   @NotEmpty(groups = PersonalChecks.class, message = "{xxx.account.userId}")
   private String userId
}


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.