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"/>
You state:
and then your validator performs:
Matcher m = p.matcher((String) value);which should result in a
ClassCastExceptionbeing 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 valueNot valid.The following snippets, for instance, work (against Primefaces 3.0M2):
The Facelet:
The Managed Bean:
If you want to check the format of the
valueagainst a regex, then it’s representation in the model should be aString, and not anInteger/int. There again, Jon Skeet’s answer is valid.[0-24]will not do what you think it is doing.