[
http://opensource.atlassian.com/projects/hibernate/browse/HV-462?page=com...
]
Pavla Nováková commented on HV-462:
-----------------------------------
Thank you Hardy for your comment, I'll try explain better the problem on solution you
suggest:
Let's start from the result we want after web form is submitted - consider user's
input and error messages that should display in form after validation:
* name=aron5 [Only letters a-z are valid for name field.] (Constraint 1a passes,
constraint 1b fails)
* email=MyTooLongEmailOverTwentyCharacters(a)somemail.com [Too long email address - maximum
is 20 characters.] (Constraint 2a fails, Constraint 2b should not be validated, since
previous step failed)
* password=SomePasswordWithLetters [Only numbers 0-9 are valid for password field.]
(Constraint 3a passes, constraint 3b fails)
Citation from Hibernate Validator manual:
"If at least one constraints fails in a sequenced group none of the constraints of
the following groups in the sequence get validated."
Using, what you suggested means, that Step1 group is validated first and it fails on email
property and so Step2 group validation is skipped. The result you get is:
* name=aron5 [MISSING ERROR MESSAGE]
* email=MyTooLongEmailOverTwentyCharacters(a)somemail.com [Too long email address - maximum
is 20 characters.] (correct)
* password=SomePasswordWithLetters [MISSING ERROR MESSAGE]
I hope I was clear enough and it does make sense.
Allow ordered validator's annotations
-------------------------------------
Key: HV-462
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HV-462
Project: Hibernate Validator
Issue Type: New Feature
Affects Versions: 4.1.0.Final
Reporter: Pavla Nováková
1) Consider domain object with simple constraints (just to demonstrate required feature,
my real example uses custom cost-expensive validation of pictures):
{code}
public class DomainObject {
@Size(max=50) // constraint 1a
@Pattern(regexp="[a-z]*") // constraint 1b
private String name;
@Size(max=20) // constraint 2a
@URL // constraint 2b
private String email;
@Size(max=100) // constraint 3a
@Pattern(regexp="[0-9]*") // constraint 3b
private String password;
{code}
2) Consider validation requirements:
* all properties should be validated together (like one virtual group)
* FOR EACH PROPERTY validation should occur in ordered steps given by order of
annotations for given property (if first validation step fails, SKIP following validation
process on given property and add validation message specific to failed / last validated
constraint for the property)
3) Why it is useful:
* simple cost-effective validation followed by cost-expensive validation (not executed if
simple constraint fails)
* we get just the "main and specific validation error" (otherwise we have to
iterate over all errors "to find the cause" or we display two or more errors for
one property which is not user-friendly according my opinion)
3) What is unusable:
* groups and group sequences (it is not possible to create proper combination of groups
and sequences for more than one property with two or more constraints)
* composed constraints (@ReportAsSingleViolation included)
** we cannot use @ReportAsSingleViolation since we want specific message for each failed
constraint
** and main problem is: if we use composed constraints all constraints are evaluated for
given property
4) Proposed solution:
* add optional order attribute to validator annotations and stop validation on first
failure if order is specified
* possibly this can be configured on global validator level with property
"skipOnFirstFailure" together with some given convention for annotation order
Please let me know if need better explanation or working examples.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira