Hi,
I would like to continue a discussion I started with Emmanuel regarding the evaluation of groups and why InvalidConstraint.getGroups() returns an array of group names, instead of the single group name under which the validation failed. The reason was that it is possible that the same field/value gets validated in multiple groups.
Emmanuel gave this use case:
Quote:
Code:
for ( InvalidConstraint ic : validator.validate(Address.class, "first", "second") ) {
System.out.println(ic.getGroups());
}
public class Address {
@NotNull(groups={"default", "first", "second"}) private String title;
}
// should print
// [first, second]
// as both are wrong
Here are my questions:
Just for clarification first - in the above example validate will return a Set with one single element and InvalidConstraint.getGroups() on this element will return all the groups in which this constraint failed, right? In this case -
first and
second. I am asking, because one could also imagine that you get a Set with two InvalidConstraint instances. One failure per group.
For that reason I was asking before why InvalidConstraint.getGroups() is not replaced by InvalidConstraint.getFailingGroup(). I was under the impression that you get one InvalidConstraint per failure.
Quote:
This is only the canonical example, I believe @GgroupSequence makes these things possible without having to explicitly list (my implementation of it at least).
I am not so sure. The whole group sequence thing got me 'confused' in the first place. Let's change our example:
Code:
@GroupSequence(name="foo", sequence={"first", "second"})
public class Address {
@NotNull(groups={"default", "first", "second"}) private String title;
}
validator.validate(Address.class, "foo");
Now the documentation says (3.4 - Group and group sequence):
"...if one of the groups processed in the sequence leads to one or more validation failures, the groups following in the sequence must not be processed..."
This means that in the above example I will only validate the group
first, since it will lead to an error.
second is not processed. So there is a semantic difference between:
Code:
validator.validate(Address.class, "foo");
and
Code:
validator.validate(Address.class, "first", "second");
I initally thought that GroupSequence was just a way to alias a list of groups. Please let me know if I still got something wrong.
My example raises another question which is related to BVAL-2. Say, I would have called the group sequence name
default - should this work? The reason I am asking is another senetence out of the spec:
"Group sequences must not use constraint declaration group names as a name. In other words, constraint definition groups cannot contain one of the group sequence names."