-->
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.  [ 2 posts ] 
Author Message
 Post subject: Validator: Adding custom reason codes / error codes
PostPosted: Wed Oct 21, 2009 2:53 am 
Newbie

Joined: Wed Oct 21, 2009 2:30 am
Posts: 1
After reading the JSR-303 and starting to write our own constraints we came to the point where we would like to add custom reason codes / error codes on constraints.
Example: If a zip code does not validate properly our system should always assign the error code "1234" to the error being added.

We considered several options but did not manage to find a proper solution yet:
- context.addError would be the optimal place during validation, but the interface is limited to message and property
- adding a constraint property "errorCode() default=1234" would be an option, but how to access it after validation? Needs reflection :-/
- adding an annotation "@ErrorCode(code=1234)" to the constraint would be an option

Of course we may have overlooked a possibility. If you happen to find a minute to give us a short advice on this we would be very happy.

Note: The error code should clearly be another thing than the error message (altough it could be a 1:1 mapping). This is the requirement we got, so we can't argue about this.


Top
 Profile  
 
 Post subject: Re: Validator: Adding custom reason codes / error codes
PostPosted: Wed Oct 21, 2009 11:00 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
What's about using the payload parameter. If I understand correctly you want to pass information (error code) through the system without really modifying/using it. That's what payload is for and that's the reason why every constraints has to define one.

You could have payload classes for each error type. The specification does contain an example were the payload is used to pass the severity of a constraint along. The online documentation is unfortunately still missing this section.

The code could look like this:
Code:
@NotNull(payload=Error.Data.class)


where Error is defined as:
Code:
public class Error {
        public static class Data implements Payload {
        }

        public static class Foo implements Payload {
        }

        public static class Bar implements Payload {
        }
}


--Hardy


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