kukudas
kukudas

Reputation: 4924

Bean Validation Min/Max wrong message

I'm using Min/Max Beanvalidation. Here is an example:

@Min(value = 100, message="too low")
@Max(value = 1000, message="too high")
private Integer example;

If i enter 99 i get the correct message "too low". If i enter 1001 i also get the correct message "too high". If i enter a very high number e.g. 10000000000 i get a generic message which i found out is this one: javax.faces.converter.BigIntegerConverter.BIGINTEGER={2}. So i suspect that if the user enters a number which is larger then the actual field type, he will get another message.

This is actually not what i want to achieve. I always want to show the user "too high". Is there a way to achieve this?

Upvotes: 1

Views: 4878

Answers (3)

Jaqen H'ghar
Jaqen H'ghar

Reputation: 4345

Why not just limit the amount of characters in the inputfield in the frontend, for example

<h:inputText maxlength="4"/>

I'd guess it's possible to bypass if you really want, but I would'nt worry too much about the usability for someone hacking the site :-)

Upvotes: 1

Hardy
Hardy

Reputation: 19109

There are really two things going on, conversion and validation. In a first step JSF needs to take your string input and convert it to a number. This is where you get the error. Your value cannot be converted to an Integer. If conversions works, JSF populates your model and that's where validation kicks in. If validation then fails you get the defined Bean Validation messages. So what can you do:

  • Configure the JSF message for javax.faces.converter.BigIntegerConverter.BIGINTEGER={2} to be more descriptive
  • Change the datatype, for example use BigInteger. In this case the conversion from string to number will work
  • Use string in the bean and validate the string. You probably need then to convert to a number at a different point though, but that depends on you use case.

Upvotes: 2

Ali Cheaito
Ali Cheaito

Reputation: 3846

The maximum for Integer in java is 2^31 which is just over 2.1 billion. The input you used, 10 billion, is then beyond the maximum of an integer and would overflow the field, so it is not a valid given the field type, regardless of any validation you may have in place. you could switch the field type to be a BigInteger, then override the default validation messages to fit your needs, but that may be overkill given the purpose of your question. You can also have custom messages

Upvotes: 1

Related Questions