0

当我从 h:selectOneMenu 更改选择时,它会在 h:inputtext 上正确呈现输出。problem occurs when I do not select anything it still showing last selection, I do not want to show anything on inputtext when "select Country" is selected

<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);
    }
}
}
4

1 回答 1

0

countryNameValidator是罪魁祸首。如果在提交表单期间验证失败,模型值将不会更新。这完全解释了为什么您一直看到最后一个成功选择的值。如果您已将 a 添加<h:messages id="messages"/>到表单并将其添加id<f:ajax render>,那么您应该已经看到验证错误消息。

现在,我检查了您的验证器。它实际上没有任何意义。required="true"它似乎与 JSF 内置验证器所做的完全相同。也许您创建它是为了防止提交“请选择”的空格值。这真的没有意义。只需使用其中一个null或一个空字符串作为“请选择”值并坚持使用required="true". 然后您可以完全删除该验证器。

但是,这并不能解决最初的问题:更改下拉列表并更新关联的文本元素时出现验证错误。如果您已将验证器更改为 a required="true",那么这实际上更容易解决。只有当真正的提交按钮被按下时才让required属性评估。true这是一个启动示例:

<h:selectOneMenu ... required="#{not empty param[submit.clientId]}">
    <f:selectItem itemLabel="Select Country" itemValue="#{null}" />
    <f:selectItems ... />
    <f:ajax listener="#{countryBean.setCountryCodeData}" render="countryCodeValue" />
</h:selectOneMenu>
<h:inputText id="countryCodeValue" ... />
...
<h:commandButton binding="#{submit}" ... />

(请注意eventexecute的属性<f:ajax>已经具有正确的默认值;我省略了它们)

于 2013-09-02T12:29:44.913 回答