The Java – JSF converter causes the validator to be ignored
This is the area:
<h:inputText id="mobilePhoneNo" value="#{newPatientBean.phoneNo}" required="true" requiredMessage="required" validator="#{mobilePhoneNumberValidator}" validatorMessage="Not valid (validator)" converter="#{mobilePhoneNumberConverter}" converterMessage="Not valid (converter)" styleClass="newPatientFormField"/>
Verifier:
@Named @ApplicationScoped public class MobilePhoneNumberValidator implements Validator,Serializable { @Override public void validate(FacesContext fc,UIComponent uic,Object o) throws ValidatorException { // This will appear in the log if/when this method is called. System.out.println("mobilePhoneNumberValidator.validate()"); UIInput in = (UIInput) uic; String value = in.getSubmittedValue() != null ? in.getSubmittedValue().toString().replace("-","").replace(" ","") : ""; if (!value.matches("04\\d{8}")) { throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,"Please enter a valid mobile phone number.",null)); } } }
When I press the command button in the form, I get the following behavior:
>When this field is empty, the message is "invalid (converter)". > When the field has a valid entry, the message is "invalid (verifier)". > When the entry of the field is invalid, the message is "invalid (converter)"
In all three cases, mobilephonenumberconverter is called getAsObject(). Mobilephonenumbervalidator. Will never be called validate(). When the field is empty, it ignores the required = "true" attribute and converts it directly
I thought the right behavior was:
>When the field is empty, the message should be required. > When a field has a valid entry, there should be no message at all. > When the entry of the field is invalid, the message should be "invalid (verifier)". > If there is a possibility that the validation passed through the transformation does not exist, the message should be "invalid (converter)"
Note: support beans are request scoped, so there is no fancy Ajax business here
to update:
It may be different from javax faces. INTERPRET_ EMPTY_ STRING_ SUBMITTED_ VALUES_ AS_ Is null set to true?
Solution
Conversion occurs before validation The converter is also called when the value is null or empty If you want to delegate a null value to the verifier, you need to design a converter. When the supplied value is null or empty, it only returns null
@Override public Object getAsObject(FacesContext context,UIComponent component,String value) { if (value == null || value.trim().isEmpty()) { return null; } // ... }
It has nothing to do with the specific problem. Your verifier is defective You should not extract submitted values from components It is different from the value returned by the converter The value correctly submitted and converted has been provided as the third method parameter
@Override public void validate(FacesContext context,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,null)); } }