Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javax empty integer field validation

My app uses javax validation for the salary field, which is int. With what annotation should I use it, to avoid the error message like that

Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'salary'; nested exception is java.lang.NumberFormatException: For input string: ""

My current implementation looks like this:

@Column(name = "salary", nullable = false)
@NotNull(message= "salary may not be empty")
@Range(min = 1)
private int salary;

I know that I can not use NotBlank for a int field, that leads to an error. How can I display the message "salary may not be empty" instead of the exception above, if the "string" is empty? Thanks

like image 795
Alex Avatar asked Sep 26 '17 11:09

Alex


1 Answers

I suggest you to use the object type Integer which can hold the null value itself.

@Column(name = "salary", nullable = false)
@NotNull(message= "salary may not be empty")
@Range(min = 1)
private Integer salary;

The above secures there will be always the salary input that has a value and it's equal or greater than 1.

like image 71
Nikolas Charalambidis Avatar answered Oct 10 '22 15:10

Nikolas Charalambidis