rburton wrote:
So taking a worked example where my constraints vary based upon the context that I'm validating against, I would need to define a 'group' of constraints for the properties that I wish to validate at that time?
E.g., my class level annotations would have something on the lines of..
class Address{
@NotNull(groups={"signup"})
@Length(max=100, groups={"contract"})
// and so on...
private String state;
}
and in the location I intend to do the validation, I would invoke the validator and provide it the group(s) I want to validate?
you are correct, except that in your example, there is no reason for @NotNull and @Length to be split into different use cases. It would be nice to come up with more advanced use cases.
But let's imagine for some reason they are split. I think in this case I would do
Code:
class Address{
@NotNull(groups={"signup", "default"})
@Length(max=100, groups={"contract", "default"})
// and so on...
private String state;
}
Because in most default cases I want to apply both (ie before the entity is saved by JPA)
Quote:
This to me seems a little hard to maintain since the validation 'group' will span multiple objects at various levels.
In such a case, it would be easier to provide a Constraints object that I could configure for a given instance.
I don't fully follow you. Can you detail your proposal?
Quote:
This leads me to go back to my initial concern that managing the context using the 'groups' attribute seems to be problematic and people will start reverting back to the old way of doing validation.
I don't yet understand why you find that problematic, surely it's easier than the old way (whatever the old way is).