Today I’ve noticed a nasty “feature” of MyFaces JSF implementation: it does not invoke custom validators on fields with no user entry.
Here’s the code from UIInput which causes the “feature”:
protected void validateValue(FacesContext context,Object convertedValue)
{
boolean empty = convertedValue == null ||
(convertedValue instanceof String
&& ((String)convertedValue).length() == 0);
if (isRequired() && empty)
{
_MessageUtils.addErrorMessage(context, this, REQUIRED_MESSAGE_ID,new Object[]{getId()});
setValid(false);
return;
}
if (!empty)
{
_ComponentUtils.callValidators(context, this, convertedValue);
}
}This means that you cannot use a custom validator to add a pretty message when the field is empty. Custom validators are only evaluated when the message is not empty. And if the value is empty it can be validated only by default validator (crap) with default error message.
The only resort to work around about this feature I found was to employ custom validation in action/actionListener code :(
First published on the old anydoby.com blog on 2007-05-11.