rym
rym

Reputation: 545

A validator with wrong result

I am using JSF2.0/primefaces

I want to create a validator, the validator should validate the format of an Input text (the Input has the type Integer).The problem is that the validator always displays Not valid.

This is the validator:

public void validateInputHours(FacesContext context, UIComponent component,
    Object value) throws ValidatorException {
Pattern p = Pattern.compile("[0-24]");
Matcher m = p.matcher((String) value);
if (!m.matches())
    throw new ValidatorException(new FacesMessage(
            FacesMessage.SEVERITY_ERROR, "Not valid",
            "Not valid"));
}

The input Text:

<p:inputText  validator="#{Control.validateInputHours}" style="width: 18px"/>

Upvotes: 1

Views: 2001

Answers (2)

Vineet Reynolds
Vineet Reynolds

Reputation: 76709

You state:

the validator should validate the format of an Input text (the Input has the type Integer)

and then your validator performs:

Matcher m = p.matcher((String) value);

which should result in a ClassCastException being thrown if you attempt to cast an Integer to a String. I suspect you are "eating" the ClassCastException somewhere, and you are issuing a generic message with value Not valid.

The following snippets, for instance, work (against Primefaces 3.0M2):


The Facelet:

<h:form>
    <p:inputText value="#{control.value}"  validator="#{control.validateInputHours}" style="width: 18px"/>
    <h:commandButton action="#{control.process}" />
    <h:messages globalOnly="true" />
</h:form>

The Managed Bean:

private int value;

public int getValue() {
    return value;
}

public void setValue(int value) {
    this.value = value;
}

...
public void validateInputHours(FacesContext context, UIComponent component,
        Object value) throws ValidatorException {
    Integer hours = (Integer) value; // the value will be the value from the model after the Apply-Request-Values phase is complete
    if (hours < 0 || hours > 24) {
        throw new ValidatorException(new FacesMessage(
                FacesMessage.SEVERITY_ERROR, "Not valid",
                "Not valid"));
    }
}

If you want to check the format of the value against a regex, then it's representation in the model should be a String, and not an Integer/int. There again, Jon Skeet's answer is valid. [0-24] will not do what you think it is doing.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1499770

Your regular expression will only match a single digit, '0', '1', '2' or '4'. I suspect that's not what you intended.

I suspect you want something like:

[0-9]|([01][0-9])|(2[0-3])

So that will match a single digit 0-9, or 00-19, or 20-23. (It's not allowing 24 at the moment - change the final "3" to a "4" if you really want to include 24 as a valid value.)

That's assuming my regex is right - it'll be something like that. That's if you really want to use a regex - an alternative would be to try to parse the value as an integer, and then check that it's in the right range.

Finally, I would expect there to be open source libraries that handle this kind of thing for you in a very simple way - it's worth checking you're not reinventing the wheel...

Upvotes: 1

Related Questions