-->
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.  [ 8 posts ] 
Author Message
 Post subject: Validate one by one
PostPosted: Tue Feb 02, 2010 9:28 am 
Newbie

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

I have a bean as following. Shortly, I am validating it and all I am printing all validation errors to screen. However before validating next one, I just want to validate in ordered sequence.

Code:
public class AccountBean {

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

///////
}


I am validating like this

Code:
Set<ConstraintViolation<Object>> constraintViolationSet = validator.validate(object, groups());


In our case I want to validate first of all NotEmpty then IsCardBannedNumber then IsValidCardNumber.... But at most I want to see just one error or instead of "Set<ConstraintViolation<Object>>" I want an ordered error set ... How can I do this?

Thanks


Top
 Profile  
 
 Post subject: Re: Validate one by one
PostPosted: Wed Feb 03, 2010 10:30 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
To solve your problem you have to look at two things. First the @GroupSequence annotation. Per default the validation order is undetermined. To enforce an order you have to create an interface like so:

Code:
@GroupSequence({NotEmpty.class, IsCardBannedNumber.class, IsValidCardNumber.class})
public interface OrderedChecks {
}


and then validate like so:

Code:
Set<ConstraintViolation<Object>> constraintViolationSet = validator.validate(object, OrderedChecks.class);


If you only want to have one single error you should look at the composing constraint feature. You would have to build a new constraint called for example @ValidCreditCard. Check the online documentation for composing constraints. Once you have a custom constraint you can use @ReportAsSingleViolation annotation to only report a single violation.

--Hardy


Top
 Profile  
 
 Post subject: Re: Validate one by one
PostPosted: Wed Feb 03, 2010 3:27 pm 
Newbie

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

Thanks for your answer. I understood the first part. But I have a little problem understanding second part. When I am using ReportAsSingleViolation, which validation message will be shown? Because in this example
I can see that it has own message?

Thanks

hardy.ferentschik wrote:
To solve your problem you have to look at two things. First the @GroupSequence annotation. Per default the validation order is undetermined. To enforce an order you have to create an interface like so:

Code:
@GroupSequence({NotEmpty.class, IsCardBannedNumber.class, IsValidCardNumber.class})
public interface OrderedChecks {
}


and then validate like so:

Code:
Set<ConstraintViolation<Object>> constraintViolationSet = validator.validate(object, OrderedChecks.class);


If you only want to have one single error you should look at the composing constraint feature. You would have to build a new constraint called for example @ValidCreditCard. Check the online documentation for composing constraints. Once you have a custom constraint you can use @ReportAsSingleViolation annotation to only report a single violation.

--Hardy


Top
 Profile  
 
 Post subject: Re: Validate one by one
PostPosted: Wed Feb 03, 2010 3:48 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
That's correct. If you use @ReportAsSingleViolation you will get the error message of the composed constraint (not the error messages from the failing constraints). I just mentioned @ReportAsSingleViolation for completeness.

I also noticed that I made a mistake in my example. So here is the right version:
Code:
public class AccountBean {
   @NotEmpty(groups = First.class, message = "{xxx.account.cc}")
   @IsCardBannedNumber(groups = Second.class, message = "{xxx.constraints.bannedCCNumber}")
   @IsValidCardNumber(groups = Third.class, message = "{xxx.account.invalidCardNumber}")
   private String creditCart;
}


Code:
@GroupSequence({First.class, Second.class, Third.class})
public interface OrderedChecks {
}


Code:
Set<ConstraintViolation<Object>> constraintViolationSet = validator.validate(object, OrderedChecks.class);


As side effect you will only get a single violation if something goes wrong. If for example @IsCardBannedNumber fails the validation will terminate without evaluating @IsValidCardNumber.

Sorry about the wrong example earlier.

--Hardy


Top
 Profile  
 
 Post subject: Re: Validate one by one
PostPosted: Wed Feb 03, 2010 4:07 pm 
Newbie

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

1 ) But what If I want to use messages of constraints instead of compound constraint message?

Thanks


Top
 Profile  
 
 Post subject: Re: Validate one by one
PostPosted: Wed Feb 03, 2010 4:12 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
In the previous example I am not using composed constraints. I am just using the groups feature in conjunction with @GroupSequence.

Composed constraint come in to play, when you would like to encapsulate the three constraints you are currently placing on the string into a single constraint like @MyCreditCardCheck. If you within the MyCreditCardCheck annotation define @ReportAsSingleVioation you would only get the error message of defined in the annotation. However, this is optional. Without @ReportAsSingleVioation the errors from the composing constraints gets propagated.

Are things clearer now?

--hardy


Top
 Profile  
 
 Post subject: Re: Validate one by one
PostPosted: Thu Feb 04, 2010 8:56 am 
Newbie

Joined: Tue Feb 02, 2010 9:00 am
Posts: 19
hardy.ferentschik wrote:
In the previous example I am not using composed constraints. I am just using the groups feature in conjunction with @GroupSequence.

Composed constraint come in to play, when you would like to encapsulate the three constraints you are currently placing on the string into a single constraint like @MyCreditCardCheck. If you within the MyCreditCardCheck annotation define @ReportAsSingleVioation you would only get the error message of defined in the annotation. However, this is optional. Without @ReportAsSingleVioation the errors from the composing constraints gets propagated.

Are things clearer now?

--hardy


Hi,

I understood clearly everything but I have a still problem


Code:

@Documented
@Constraint(validatedBy = { })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@ReportAsSingleViolation
@NotNull
@Size(min = 1)
public @interface NotEmpty {
   String message() default "{org.hibernate.validator.constraints.NotEmpty.message}";

   Class<?>[] groups() default { };

   Class<? extends Payload>[] payload() default { };
}


In case a error, It is raising error message of "org.hibernate.validator.constraints.NotEmpty.message", but I want to delegate this error message to checked constraints. For example, in case a NotNull error, I want to show its error message.

I hope that my question is clear?

Thanks


Top
 Profile  
 
 Post subject: Re: Validate one by one
PostPosted: Thu Feb 04, 2010 10:34 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
You would have to remove @ReportAsSingleViolation. Of course you cannot do this in the built-in NotEmpty constraint. If you need this functionality for the NotEmpty constraint you will have to create a MyNotEmpty constraint.


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