0

I'm having issues with JSR 303 validation when the user does not enter anything in a box. I want to validate that a field must be a number and > 0. Here is the code

@Min(value = 1)
private float price;

If a number is entered the validation works fine but if left empty throws NumberFormatException

I know that I can change float to Float and use @NotEmpty but I don't want to change the type just because of this simple validation.

Any ideas?

Smith
  • 1,431
  • 4
  • 18
  • 26
  • see http://stackoverflow.com/questions/14976566/spring-initbinder-bind-empty-or-null-values-of-a-float-field-as-0 for a solution. Basically, you have to extend CustomNumberEditor and inside setAsText you set value to 0 if incoming text is empty. – ksokol Mar 29 '14 at 16:28

1 Answers1

0

Have a look here:

http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html

It'll restrict user to enter only things, that you are allowing.

Ysr Shk
  • 224
  • 5
  • 16