Tôi đang sử dụng JSF2 trên GlassFish 3.Không nên bỏ qua xác thực khi không có giá trị nào được chỉ định?
Tôi có biểu mẫu chấp nhận và số điện thoại tùy chọn. Tôi có trình xác thực số điện thoại tùy chỉnh này (bên dưới) và tôi có trường được đặt thành required = "false" vì số điện thoại là tùy chọn trong biểu mẫu.
Vấn đề là, giá trị trong trường luôn được xác thực. Không nên bỏ qua xác thực khi không có giá trị nào được chỉ định?
Có điều gì đó tôi đang làm sai. Bất kỳ trợ giúp được đánh giá cao, cảm ơn!
<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);
}
}
}