私はGlassFish 3でJSF2を使用しています。値が指定されていない場合、検証をスキップしないでください。
私は受け取る電話番号とオプションの電話番号を持っています。私はこのカスタム電話番号バリデーター(下記)を持っています。電話番号はこのフォームではオプションであるため、フィールドをrequired = "false"に設定しています。
問題は、フィールドの値が常に検証されることです。値が指定されていない場合、検証をスキップしないでください。
私は何か間違っている必要があります。どんな助けでも感謝しています! JSF 2.0から
<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);
}
}
}