So I have a String I need to check for a valid format for which I used a pattern, and whether the entered value is valid according to some business rules for which I used a custom validator. Now the customer would prefer it if the error message associated with the latter is only shown if the former didn’t occur. But as far as I know there is no way to make this distinction.
Or can anyone think of a way to do this?
So I have a String I need to check for a valid format for
Share
The trick is: have two different constraints (annotations),
By default the
javax.validation.constraints.Pattern“validator” accept null.So what you need to do in the end is this:
Added:
(Comment)
In this case you can use the so called
groups(See JSR303 Bean Validation Spec, Chapter 4.1.2 groups — there is also an example). Use the default Group for@Patternand an other group for@NotNull. Now you can enable or disable the Validation rules according the groups you specify for validation.There is one problem left: in Spring 3.0 you can not Specify the Group you want to use for the automatic validation process. But in 3.1 you can, according to that Feature Request SPR-6373 (I have not tryed it, but I hope it works)