Sto usando JSF2 su GlassFish 3.Non si dovrebbe saltare la convalida quando non è specificato alcun valore?
Ho un modulo che accetta e numero di telefono opzionale. Ho questo validatore del numero di telefono personalizzato (sotto), e ho il campo impostato su required = "false" perché il numero di telefono è opzionale nel modulo.
Il problema è che il valore nel campo viene sempre convalidato. Non dovrebbe essere saltata la validazione quando non è specificato alcun valore?
Deve esserci qualcosa che sto sbagliando. Qualsiasi aiuto è apprezzato, grazie!
<h:outputText value="#{msg.profile_otherPhone1Label}#{msg.colon}" />
<h:panelGroup>
<p:inputText label="#{msg.profile_otherPhone1Label}" id="otherPhone1" value="#{profileHandler.profileBean.otherPhone1}" required="false">
<f:validator validatorId="phoneValidator" />
</p:inputText>
<p:spacer width="12"/>
<h:outputText value="#{msg.profile_phoneExample}" />
</h:panelGroup>
#
public class PhoneValidator implements Validator {
@Override
public void validate(FacesContext facesContext, UIComponent uIComponent,
Object object) throws ValidatorException {
String phone = (String) object;
// count the digits; must have at least 9 digits to be a valid phone number
// note: we're not enforcing the format because there can be a lot of variation
int iDigitCount = 0;
for (char c : phone.toCharArray()) {
if (Character.isDigit(c)) {
iDigitCount++;
}
}
if (iDigitCount < 9) {
FacesMessage message = new FacesMessage();
message.setSummary(Messages.getMessage("error_phoneNumberNotValid"));
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
}
}