I have a bean where I define multiple validation annotations for each field e.g.
@NotEmpty
@Pattern(regexp="(\\-?\\d)+")
@Min(value=1)
String myField;
I have encountered two one problem that I cannot resolve in any easy way.
Edit. I tried to implement custom composite constraint with @ReportAsSingleViolation. The problem is that it will report the same message for all violations involved in composition. This is not what I need.
Suggestions, please?
It should be possible to create the behavior you want to achieve using a combination of validation groups and a fully defined validation group sequence
Quoting from JSR-349 (aka. BeanValidation 1.1) spec, paragraph 4.4.2
Processing a group is defined in Section 4.6 ; if one of the groups processed in the sequence generates one or more constraint violations, the groups following in the sequence must not be processed. This ensures that a set of constraint is evaluated only if another set of constraint is valid.
highlighting by me
This should be relatively easy, going by the Group Sequence Example
@GroupSequence({YourClass.class, Second.class, Third.class})
public class YourClass {
@NotNull
@Pattern(regexp="(\\-?\\d)+", groups=Second.class)
@Min(value=1, groups=Third.class)
String myField;
}
with Second
and Third
defined as simple marker-interfaces should do the trick. Be aware that this does not validate all fields until the first constraint violation, but just until the first overall violation.
This means some of your fields may become invalid after fixing other fields' violations.
If all else fails you can still reimplement the behavioral components, your Validation Provider.
You should be able to provide your own implementation of the ConstraintValidatorContext
by using a validation.xml
Be aware you'd have to break the contract of ConstraintValidatorContext.ConstraintViolationBuilder
:
To create the ConstraintViolation, one must call either one of the addConstraintViolation() methods available in one of the interfaces of the fluent API.
If another method is called after addConstraintViolation() on ConstraintViolationBuilder or any of its associated objects an
IllegalStateException
is raised.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With