Here\'s the field:
Conversion happens before validation. Converters will also be called when the value is null
or empty. If you want to delegate the null
value to the validators, then you need to design your converters that it just returns null
when the supplied value is null
or empty.
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || value.trim().isEmpty()) {
return null;
}
// ...
}
Unrelated to the concrete problem, your validator has a flaw. You should not extract the submitted value from the component. It's not the same value as returned by the converter. The right submitted and converted value is available as the 3rd method argument already.
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
if (value == null) {
return; // This should normally not be hit when required="true" is set.
}
String phoneNumber = (String) value; // You need to cast it to the same type as returned by Converter, if any.
if (!phoneNumber.matches("04\\d{8}")) {
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please enter a valid mobile phone number.", null));
}
}