-->
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.  [ 3 posts ] 
Author Message
 Post subject: Validation and Formatting
PostPosted: Mon Jan 14, 2013 4:30 pm 
Newbie

Joined: Mon Jan 14, 2013 4:07 pm
Posts: 17
I need to validate an entity field as a valid phone number and then format it as E.164. The formatting code currently does the validation in the library I am using. I would like both to be done in a way that throws a ConstraintViolationException. I naively did this with a @PhoneNumber validator before I realized that I also wanted to format the argument to the setter method.

Is there any simple way to do this? It seems tedious to create ConstrainViolations for a ConstraintViolationException programmatically, otherwise I could just get rid of the annotation and have a #format method throw the validation exception. Thoughts? I can't imagine this is the only time where someone would want to format and validate at the same time.

R.


Top
 Profile  
 
 Post subject: Re: Validation and Formatting
PostPosted: Tue Jan 15, 2013 1:39 pm 
Hibernate Team
Hibernate Team

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

Could you post some example code of what you want to achieve? I'm not sure whether I've completely understood what you're up to. Note that ConstraintViolation is not intended to be instantiated by the user, this is done by the Bean Validation provider. Maybe it helps if you extract the pattern for validation/formatting into a constant which can be referenced from within the validator and the formatting method?

--Gunnar

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


Top
 Profile  
 
 Post subject: Re: Validation and Formatting
PostPosted: Tue Jan 15, 2013 1:52 pm 
Newbie

Joined: Mon Jan 14, 2013 4:07 pm
Posts: 17
Hi Gunnar,

What I want to do is pretty simple. Let's say I have an entity with various Bean Validation constraints such as @NotNull, @Email, etc. During a REST call, if I get a ValidationConstraintException I can easily parse the ConstraintViolations into "field" and "cause" properties in a JSON error. I want exception handling to be simple and the above is very simple to do.

Now enter Phone Number. For Phone Number I need to do two things on the Entity Bean. I need to ensure that it is a valid phone number and then I need to format it into an E.164 string. Using Google's libphonenumber, that may look like this:

Code:
     Contact.java:
     public void setPhoneNumber( String phoneNumber ) {
         this.phoneNumber = PhoneNumberUtils.format( phoneNumber, PhoneNumberType.E164 );
     }


The format function will take something like "(503) 223-1414" and turn it into E.164 format "+15032231414". The format method does two things, ensure the input is valid and then format it appropriately for storage in the database. If the input is invalid it will throw a NumberFormatException.

What I would like is to use the same pattern for all validation -- email, phone number, null fields, etc. But for "phoneNumber" I have to add special handling for NumberFormatException anywhere I use it. So, I prefer NOT to add special catch blocks anywhere I use the Contact entity to catch both ConstraintViolationExceptions AND NumberFormatExceptions.

Does this help explain the issue?

R.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.