Issue Details (XML | Word | Printable)

Key: HV-5
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Unassigned
Reporter: Edwin van der Elst
Votes: 2
Watchers: 3
Operations

If you were logged in you would be able to see more operations.
Hibernate Validator

Multiple validators of the same type per element (John Gilbert)

Created: 21/Dec/06 02:10 AM   Updated: 20/Jul/09 09:43 AM   Resolved: 13/Feb/07 12:45 PM
Component/s: validators
Affects Version/s: Bundle 3.2.1
Fix Version/s: 3.0.0.ga

Time Tracking:
Not Specified

File Attachments: 1. Java Source File ClassValidator.java (20 kB)
2. Text File validators_patch.txt (11 kB)


Participants: Edwin van der Elst, Emmanuel Bernard and John Gilbert


 Description  « Hide

I have a suggestion for the following problem:
I created an Validator to check if a 'date'> 'another date'

The annotation looks like this:
@After(property="endDate", after="beginDate", message="End should be after begin")

Since it involves 2 properties, it is a class-level validation.

Now....
I want multiple 'after' validations on a single bean.
But that is not possible (you can only place an annotation once on a class).

My proposal is to add an annotation 'Validations':

@Validations( {@After(...), @After(..)} )

I could do the multiple validations in a single validatorclass, but I want all the messages added to the invalidValues array, not a single message.

This change would also require changes in the ClassValidator class.

What do you think of this proposal? Should I add it to Jira (I can propably implement it if there are no objections to the required changes)



Emmanuel Bernard added a comment - 04/Jan/07 06:07 AM

Edwin,
I believe you can implement an untyped version

@GenericValidator( class=..., parameters = @Params(key=, value=) )
Then @Validators(GenericValidator[] value() )

Hibernate Annotations has the ability to "create" annotations (ie recreate the actual validator).

have a look at org.hibernate.test.annotationfactory.AnnotationFactoryTest


Edwin van der Elst added a comment - 05/Jan/07 08:12 AM

Hereby a patch for ValidatorList (based on latest svn checkout)
Also included, a test-case for a 'Loan' class and a bean validatorclass 'After'.

Feel free to comment on the implementation

Edwin


John Gilbert added a comment - 07/Feb/07 05:40 PM

I have the same problem but the GenericValidator seems very verbos. Here is an example of something I am playing with:

@Expressions(
{ @Expression(value = "value.field1 ne value.field2", message = "Field1 must not equal Field2."), @Expression(value = "value.field3 > value.field4", message = "Field3 must be greater than Field4.") })
public class Entity {
private String field1;
private String field2;
private Integer field3;
private Integer field4;
...
}

Ignore the fact that I am using expression language.

The Expressions annotation is not linked to a ValidatorClass, but the Expression annotation is.

public @interface Expressions {
Expression[] value();
}

@ValidatorClass(ExpressionValidator.class)
public @interface Expression {

String value();
String message() default "";
}

In ClassValidator when it initializes it gets all the annotations off of the class and members and checks if they have a ValidatorClass. It could also use reflection to see if the annotations aggregate other annotations which in turn are linked to a ValidatorClass.

This would require a moderate bit of rework to ClassValidator, but would make using it much cleaner.

Thoughts?


Emmanuel Bernard added a comment - 08/Feb/07 12:37 AM

Problem is the user needs to create the Annotation container for each specific type.
I guess that's the price to pay, the solution is more elegant when reading the code.
the algorithm should check if the return type of value() is a array of annotations. checking all the members make few sense, I think.
I like the el annotation btw, I'm waiting for the unified el spec.


John Gilbert added a comment - 08/Feb/07 12:41 PM

This was easier than I thought it would be. I just added the handleAggregateAnnotations() method and called it in two places.

NOTE: The source I attached is from 3.2.0.CR1, because I am still using and older version of the jboss embedded container.

Btw, I would be glad to donate the EL validator, but I am using commons-jexl.


Emmanuel Bernard added a comment - 13/Feb/07 12:45 PM

Commited John's patch. I don't think Edwin's solution is needed anymore.
Thanks to both of you guys


John Gilbert added a comment - 13/Feb/07 01:07 PM

Great! I have created a blog entry that talks about this topic:

http://jroller.com/page/jgilbert01?entry=extending_the_hibernate_validation_framework


Emmanuel Bernard added a comment - 13/Feb/07 01:47 PM

cool, nice ideas. I like the EL actually


Edwin van der Elst added a comment - 14/Feb/07 02:38 AM

I like John's solution better too