मैं ग्लासफ़िश पर जेएसएफ 2 का उपयोग कर रहा हूं 3.कोई मान निर्दिष्ट नहीं होने पर सत्यापन को छोड़ दिया जाना चाहिए?
मेरे पास एक ऐसा फॉर्म है जो स्वीकार करता है और वैकल्पिक फोन नंबर। मेरे पास यह कस्टम फोन नंबर सत्यापनकर्ता है (नीचे), और मेरे पास आवश्यक फ़ील्ड सेट है = "झूठी" क्योंकि फ़ोन नंबर फॉर्म में वैकल्पिक है।
समस्या यह है कि, क्षेत्र में मूल्य हमेशा मान्य हो रहा है। निर्दिष्ट मूल्य नहीं होने पर सत्यापन को छोड़ना नहीं चाहिए?
कुछ ऐसा होना चाहिए जो मैं गलत कर रहा हूं। हर प्रकार की सहायता का स्वागत है, धन्यवाद!
<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);
}
}
}