I have an object.
public class MyObject
{
....
@Column(name = "a_number") @NotNull @NumberFormat(style = Style.NUMBER) @Min(1)
private Integer aNumber;
...
//getters and setters
}
In my controller I have @Valid annotation on my object being posted. I do have validation working on all my other fields in the class (their all Strings) except this number. If I enter a number from my form it works fine and if I violate the @Min(1) it also gives me the correct validation error. My problem however is that if you enter a string instead of a number it throw a NumberFormatException.
I’ve seen many examples of Integer and validation but no one accounts for if you enter a string into the form being posted. Do I need to do the validation else where? Javascript? I would like a solution that falls in line with the rest of spring validation so I could use this in other classes. I would just like an error stating it must be numeric. Also I tried using the @Pattern annotation but apparently thats just for strings.
Suggestions?
You can add the following to your file which controls your error messages (these are the generic ones it looks for in the case of a type mismatch: