h:selectOneMenu から選択を変更すると、h:inputtext で出力が正しくレンダリングされます。何も選択しないと問題が発生します
<h:selectOneMenu id="country_id" value="#{countryBean.countryCode}" >
<f:validator for="country_id" validatorId="countryNameValidator"/>
<f:selectItem itemLabel="Select Country" itemValue=" "/>
<f:selectItems value="#{countryBean.countryList}" var="countryDTO" itemLabel="#{countryDTO.countryName}(#{countryDTO.countryCode})" itemValue="#{countryDTO.countryCode}"/>
<f:ajax event="change" render="countryCodeValue" execute="@this" listener="#{countryBean.setCountryCodeData}"></f:ajax>
</h:selectOneMenu>
<h:inputText disabled="true" value="#{countryBean.countryCode}" id="countryCodeValue" required="true" >
</h:inputText>
customCountryNameValidator.java
package com.mcd.webex.custom.validation;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
@FacesValidator(value="countryNameValidator")
public class CustomCountryValidator implements Validator{
@SuppressWarnings("unused")
public void validate(FacesContext fContext, UIComponent uiComponent, Object value)throws ValidatorException {
String countryCode = (String) value;
System.out.println("from Country Validation message:"+countryCode);
FacesContext fc = FacesContext.getCurrentInstance();
System.out.println("Country Code length: "+countryCode.trim().length());
if(null != countryCode && countryCode.trim().length() == 0){
FacesMessage message = new FacesMessage("Country validation failed.","Please select country.");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}else if(null == countryCode){
FacesMessage message = new FacesMessage("Country validation failed.","Please select country.");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
}
}