jsr 303 validate a number and auto initialize to 0
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 wi开发者_开发知识库th
@NumberFormat(style = Style.NUMBER)
How to go about default initilizing it to 0?
How about assigning the field an initial value?
If the field is of type int
it automatically has a value of 0
.
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.
Just do:
validation rules
private int numberToValidate = 0;
精彩评论