-->
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: validation annotations with OR?
PostPosted: Fri Jul 08, 2011 8:47 am 
Newbie

Joined: Fri Jul 08, 2011 8:45 am
Posts: 6
Hi

I've got a pretty simple validation requirement for a field. I've read the Hibernate Validator reference, but I still can't figure out how to do this.

I want to specify that a field should EITHER be empty, OR contain at least nine characters.

How do I do this?

Thanks for your help.

Bruno.


Top
 Profile  
 
 Post subject: Re: validation annotations with OR?
PostPosted: Fri Jul 08, 2011 2:45 pm 
Hibernate Team
Hibernate Team

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

do you mean by empty an empty string ("") or actually null? In case of the latter you could simply use the @Size annotation, as all built-in constraints allow null as valid value. Assuming you're validating values from an UI there might be an option supported by your UI framework which automatically converts empty strings to null. In JSF 2 for example there is the option javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL.

In case you really have to deal with empty strings you might use the @Pattern constraint with a regular expression which accepts the empty string or requires at least 9 characters such as this:

Code:
@Pattern(regexp = "^|.{9,}")
String myString;


Hope that helps,

--Gunnar

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


Top
 Profile  
 
 Post subject: Re: validation annotations with OR?
PostPosted: Fri Jul 08, 2011 3:12 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Just thought a bit more about this :)

If you're using HV 4.2 you could also create a reusable constraint @EmptyOrSize based on the boolean constraint composition feature introduced with 4.2. This could roughly look like this:

Code:
@ConstraintComposition(CompositionType.OR)
@Pattern(regexp = "")
@Size
@ReportAsSingleViolation
@Target({ ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = { })
public @interface EmptyOrSize {

   String message() default "{EmptyOrSize.message}";
   Class<?>[] groups() default { };
   Class<? extends Payload>[] payload() default { };
   
   @OverridesAttribute(constraint = Size.class, name = "min")
   int min() default 0;
   
   @OverridesAttribute(constraint = Size.class, name = "max")
   int max() default Integer.MAX_VALUE;

}

This constraint could be used like this:

Code:
@EmptyOrSize(min=9)
String myString;

--Gunnar

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


Top
 Profile  
 
 Post subject: Re: validation annotations with OR?
PostPosted: Sat Jul 09, 2011 4:06 am 
Newbie

Joined: Fri Jul 08, 2011 8:45 am
Posts: 6
Hi Gunnar

Brilliant!

In fact, I use a binder with StringTrimmerEditor. I trim, but I don't convert to null, because I want to be able to distinguish between fields that have been set to empty, and those that have never been set.

And I was being stupid - of course, @Pattern is always going to be flexible enough to cope with these situations! I just have to get my regexp head on.

The custom constraint is the most elegant, especially because I can see this type of constraint being used in multiple places in the application. I'm currently using HV 4.1. I'll see if I can shift to 4.2 without breaking anything.

Thank you very much for this.

--Bruno.


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.