I have a field in a Java Bean (kilograms) that is not mandatory, but if the user does enter it, it obviously needs to be a number.
Is there a combination of JSR303 annotations that can enforce that rule? I tried
@Min(value=0)
But that obviously disallows empty, which is allowable.
Thanks for any help.
You cannot do a OR composition with Bean Validation 1.0. Hibernate Validator has a feature which allows you do so. It is called “boolean composition of custom constraints” – http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#d0e3701
So you need a custom constraint and you obviously won’t be standard conform, but it works.
For Bean Validation 1.1 there is a discussion whether boolean composition should make it into the standard (BVAL-260), but it is not guaranteed.