I am attempting to validate a Double
field with the help of Hibernate validation API - using @Digits
annotation
<areaLength>0</areaLength>
<buildArea>0.0</buildArea>
@Digits(integer=10, fraction=0)
private Long areaLength = null; // Here areaLength = 0
@Digits(integer=20, fraction=0)
private Double buildArea = null; // Here buildArea = 0.0
Here areaLength
has no constraint violation,
but buildArea
is getting a constraint violation, saying
buildArea numeric value out of bounds (<20 digits>.<0 digits> expected)
No violation for 10.0, But getting violation for 0.0.
Does anybody know the reason?
Full Code :
public class ValidationTest {
public static void main(String a[]) {
Vehicle vehBean = new Vehicle();
try {
if (vehBean != null) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Vehicle>> violations = validator.validate(vehBean);
if (violations!= null && violations.size() > 0) {
for (ConstraintViolation<Vehicle> violation : violations) {
System.out.println("Validation problem : " + violation.getMessage());
}
}
}
} catch(Exception e) {
throw e;
}
}
}
class Vehicle {
@Digits(integer=20, fraction=0)
private Double buildArea = 0.0;
}
Validation problem : numeric value out of bounds (<20 digits>.<0 digits> expected)
You've restricted the amount of digits for fraction part to 0
meaning no fraction allowed, even though there isn't any in your 0.0
but you should confirm that that is the case, I think you have a Double
value that is 0 < x < 0.1
You can also try using Float
for the same field, and check if that has the same trouble?
The value of the field or property must be a number within a specified range. The integer element specifies the maximum integral digits for the number, and the fraction element specifies the maximum fractional digits for the number.