-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Validator Improvement
PostPosted: Mon Sep 19, 2005 5:00 am 
Newbie

Joined: Sat Jul 30, 2005 3:11 am
Posts: 4
Hibernate version:annotation 3.1 beta 5

I think most of the time we want to process org.hibernate.validator.InvalidValue arrays, before displaying them to the user. For example we may need to do certain tasks on occurence of each error, or more simply we don't want to show all error messages to the user.

So, I think adding a field containing the annotation class of the constraint to InvalidValue is very useful.

--Mehran
thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 19, 2005 5:27 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't understand

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 5:37 am 
Beginner
Beginner

Joined: Thu Jan 06, 2005 6:11 am
Posts: 32
Location: Magdeburg, Germany
Some more suggestions:

I'm now starting to write a custom input validator for tapestry that is making use of the hibernate validation framework, so I don't have to keep track of the same validation rules in several places.

One thing that is really needed is a function that checks for possible invalid values before they get set:

Code:
public InvalidValue[] getInvalidValues(T bean, String propertyName, Object value);


(I really needed this and solved it by overriding ClassValidator and adding it by hand...)

At present there is no problem in passing null for the bean (as to check for values before bean creation, e.g. a user registration form), I really hope it doesn't change.

Also it would be nice to have a common place for existing validators, maybe in a hashmap in the ClassValidator class:

Code:
public static ClassValidator getValidatorForClass(Class class);
public static ClassValidator getValidatorForClass(Class class, boolean createNew);


If ClassValidators are threadsafe (I'm not shure about it), could be done like that:

Code:
private static Map<Class, ClassValidator> validators = new HashMap<Class, ClassValidator>();
private static Boolean monitor = new Boolean(true);

public static ClassValidator getValidatorForClass(Class class){

ClassValidator validator = validators.get(class);
         if (validator == null) {
            synchronized (monitor) {
               validator = validators.get(class);
               if (validator == null) {
                  validator = new ClassValidator(class);
                  validators.put(class, validator);
               }
            }
         }
return validator;
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 5:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Hildolfur wrote:
One thing that is really needed is a function that checks for possible invalid values before they get set:

Code:
public InvalidValue[] getInvalidValues(T bean, String propertyName, Object value);


(I really needed this and solved it by overriding ClassValidator and adding it by hand...)


Interesting. Add a feature request to JIRA.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 6:26 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I keep track of my classvalidator in an other layer (the hib events for example). I think it's fine.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 6:46 am 
Beginner
Beginner

Joined: Thu Jan 06, 2005 6:11 am
Posts: 32
Location: Magdeburg, Germany
For now I have the above mentioned tapestry validator and a hibernate validator aspect, performing validation of business object setters, fields and public constructors before they are executed (except for the private empty constructor that is used by hibernate). This way it's impossible to create invalid business objects throughout the application. (Ok, it's still slightly buggy for the setter methods, but works for me).

I thought it would be good to keep track of Validators at one location throughout the application, since I use the validators in separate layers.

Of cource, I could write a ValidatorUtils class for it, but I think it would be nicer to put it directly in ClassValidator.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 7:53 am 
Beginner
Beginner

Joined: Thu Jan 06, 2005 6:11 am
Posts: 32
Location: Magdeburg, Germany
Forget about my static getValidatorForClass approach - it's still not thread safe as HashMap get is not thread safe also...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 12:44 pm 
Beginner
Beginner

Joined: Thu Jan 06, 2005 6:11 am
Posts: 32
Location: Magdeburg, Germany
Using ConcurrentHashMap should work.

What about fetching validation constraints from interfaces also?

The class could be annotated with information about the interfaces it should also take validation rules from.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 08, 2005 4:12 pm 
Beginner
Beginner

Joined: Thu Jan 06, 2005 6:11 am
Posts: 32
Location: Magdeburg, Germany
At present I'm able to define a custom localizable message on every annotation, but it would be convenient to have custom default localizable messages for every validator, though I have no idea how to implement it.

So if the right language file is provided, I can get custom default validator messages - for the user interface the not null validator might return "is a mandatory field", for BO validation the default "may not be null" might apply.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 09, 2005 12:40 pm 
Beginner
Beginner

Joined: Thu Jan 06, 2005 6:11 am
Posts: 32
Location: Magdeburg, Germany
implemented it myself, so default messages may be set in the resource bundle, using class name of annotation class as the key:

org.hibernate.validator.NotNull=is a mandatory field

If key is not present, default from validator is used.

Code:
   private Validator createValidator(Annotation annotation) {
      try {
         ValidatorClass validatorClass = annotation.annotationType()
               .getAnnotation(ValidatorClass.class);
         if (validatorClass == null) {
            return null;
         }
         Validator beanValidator = validatorClass.value().newInstance();
         beanValidator.initialize(annotation);
         String messageTemplate = (String) annotation.getClass().getMethod(
               "message", (Class[]) null).invoke(annotation);
         String defaultMessage = (String) annotation.annotationType()
               .getMethod("message", (Class[]) null).getDefaultValue();
         try {
            // try to replace the default message with a message from the
            // resource bundle if a key with the class name of the annotation
            // is available
            if ((messageTemplate.equals(defaultMessage))
                  && (messageBundle != null)
                  && (messageBundle.getString(annotation.annotationType()
                        .getName()) != null)) {
               messageTemplate = "{"
                     + annotation.annotationType().getName() + "}";
            }
         } catch (MissingResourceException ex) {
            // key not present, use default message from validator
         }
         String message = replace(messageTemplate, annotation);
         messages.put(beanValidator, message);
         return beanValidator;
      } catch (Exception e) {
         throw new IllegalArgumentException(
               "could not instantiate ClassValidator", e);
      }
   }


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 09, 2005 4:46 pm 
Beginner
Beginner

Joined: Thu Jan 06, 2005 6:11 am
Posts: 32
Location: Magdeburg, Germany
@emmanuel: I think in the top posting of this thread mehranmehr wants to have a property on InvalidState containing the annotation class of the annotation that caused to the error so he can react on different errors accordingly.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2005 1:59 am 
Newbie

Joined: Sat Jul 30, 2005 3:11 am
Posts: 4
Hildolfur wrote:
@emmanuel: I think in the top posting of this thread mehranmehr wants to have a property on InvalidState containing the annotation class of the annotation that caused to the error so he can react on different errors accordingly.


that's right
thank you


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2005 4:56 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
mehranmehr wrote:
Hildolfur wrote:
@emmanuel: I think in the top posting of this thread mehranmehr wants to have a property on InvalidState containing the annotation class of the annotation that caused to the error so he can react on different errors accordingly.


that's right
thank you


What I meant is that I don't fully buy the usefulness of this feature. Can you describe a real use case needing that. Plus imagine at some point we externalize the constraint def in an XML file, there is no more annotation to provide

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2005 6:26 am 
Beginner
Beginner

Joined: Thu Jan 06, 2005 6:11 am
Posts: 32
Location: Magdeburg, Germany
hmm, any comments on default validation messages and validator constraints on interfaces? Will this be implemented? Or is it outside the scope of hibernate validator?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 7:55 am 
Beginner
Beginner

Joined: Thu Jan 06, 2005 6:11 am
Posts: 32
Location: Magdeburg, Germany
@Hibernate Team: What about interfaces and customizable default messages?

If I'm writing crap, it's ok to tell me, or if I should better submit a feature request to JIRA , or anything else, but no feedback at all is difficult to handle...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.