jsr 303 validate a number and auto initialize to 0
-
30-09-2019 - |
Question
in my domain class I have an integer which is being populated from form. if user enters any digit , it will be validated and if he leaves the input box empty, it will default initialize to 0
so far I have done the first part with
@NumberFormat(style = Style.NUMBER)
How to go about default initilizing it to 0?
Solution
If youfr probldem is that the default value is 0 when the user does not enter anything in the box, set a validator for the accepted range, and initialize it to a number outside that range. If all numbers are acceptable, change it from an int to an Integer, and use @NotNull.
OTHER TIPS
How about assigning the field an initial value?
If the field is of type int
it automatically has a value of 0
.
Just do:
validation rules
private int numberToValidate = 0;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow