javing
javing

Reputation: 12433

Validating int value

I am having difficulties doing a simple validation of a text field that has as a value - an int variable.

I need the following;

This is the validator I created, but it does not work as I desire. Can you have a look at it and tell me where I am making the mistake?

public void validateProductValue(FacesContext context,
            UIComponent validate, Object value) {
        FacesMessage msg = new FacesMessage("");
        String inputFromField = (String) value;
        String simpleTextPatternText = "^([0-9]+$)?";
        Pattern textPattern = null;
        Matcher productValueMatcher = null;
        textPattern = Pattern.compile(simpleTextPatternText);
        productValueMatcher = textPattern.matcher(inputFromField);      

        if (!productValueMatcher.matches()) {
            msg = new FacesMessage("Only digits allowed");
            throw new ValidatorException(msg);
        }

        if (inputFromField.length() <= 0) {
            msg = new FacesMessage("You must enter a value greater than 0");
            throw new ValidatorException(msg);
        }

        if (Integer.parseInt(inputFromField.toString().trim()) <= 0) {
            msg = new FacesMessage("0 or bellow is not permited");
            throw new ValidatorException(msg);
        }       
    }

This is how I call the field:

<h:inputText id="productValue" value="#{newOfferSupportController.productValue}" validator="#{newOfferSupportController.validateProductValue}"/>

This the validation shown in the browser: enter image description here

Text

/newoffer.xhtml @44,184 
validator="#{newOfferSupportController.validateProductValue}":
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

Upvotes: 1

Views: 11287

Answers (3)

Thomas
Thomas

Reputation: 88757

First of all your regex is wrong. You need to put $ at the end, like this ^([0-9]+)?$.

This doesn't work as you might expect:

if (inputFromField.length() <= 0) {
  msg = new FacesMessage("You must enter a value greater than 0");
  throw new ValidatorException(msg);
}

inputFormField.length() is the length of the text, which can't be less than 0 and even if it were greater than 0 you could enter a negative value, e.g. "-1" which has length 2.

If I see the exception correctly, you're getting a ClassCastException. Take a look at line 44 (which you are told by the exception message), which I guess is String inputFromField = (String) value; Did you define a converter for that field? If so value might be an Integer and not a String.

Edit:

Note that your validator actually tries to do two things: convert the input to an integer and validate the integer value. In JSF you normally have two classes that do this:

  • first a converter is called in order to convert the string to an integer and the other way round, especially if your model field is an Integer - this is where you'd check if you actually got an integer
  • second you validate the integer value, e.g. by enforcing a minimum - here you're using a validator

Also note that there are already built in validators, that do what you want. Take a look at <f:validateLongRange minimum = "0"/> for example.

Upvotes: 1

Clayton
Clayton

Reputation: 5350

You should be able to enforce the conditions your looking just using a regular expression: ^[1-9]+[0-9]*$

Upvotes: 1

Richard H
Richard H

Reputation: 39135

First, is value actually an instance of String? Or can it be cast to an Integer?

If not, do this:

if (value == null) {
   throw new ValidatorException("No input value");
}

String inputValue = (String) value;

try {
   int v = Integer.parse(inputValue);

   if (v <= 0) {
      throw new ValidatorException("Input less than or equal to 0");
   }     


} catch (NumberFormatException e) {
   throw new ValidatorException("Input is not an integer");
}

Upvotes: 0

Related Questions