使用valueChangeListener后,验证不再有效

时间:2013-12-03 08:32:43

标签: validation jsf primefaces

大家好我有一些代码,如果用户选择国家是英国然后它显示邮政编码字段,这是有效的,但是因为我已经添加了这个我再也无法让验证器工作来验证邮政编码是正确的布局,也不能使required = true工作,这在添加valueChangeListener之前一切正常

这是代码:

<p:outputLabel value="#{bundle.country}" for="Countries" />
<p:selectOneMenu id="Countries" 
     value="#{bean.addressToCreate.country}"                                     
     valueChangeListener="#{country.countryLocaleCodeChanged}" 
     filter="true" 
     filterMatchMode="startsWith">
   <p:ajax update="postcodePanel" event="valueChange"/>
   <f:selectItems value="#{country.countryInMap}" />
</p:selectOneMenu>

<p:outputPanel id="postcodePanel">
  <p:outputLabel value="#{bundle.labelPostcode}" for="Postcode" />
  <p:inputText rendered="#{bean.addressToCreate.country=='United Kingdom'}" 
      id="Postcode" 
      validator="PostcodeValidator" 
      value="#{bean.addressToCreate.postcode}" 
      title="#{bundle.labelPostcode}" 
      required ="True"
      requiredMessage="#{bundle.requiredPostcode}" />
</p:outputPanel>

修改

country.countryLocaleCodeChanged

    package sws.control;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ValueChangeEvent;

@ManagedBean(name = "country")
@SessionScoped
public class CountryBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private static Map<String, String> countries;

    private String localeCode = "United Kingdom"; //default value 

    static {
        countries = new LinkedHashMap<String, String>();
        //countries.put("United Kingdom", "United Kingdom"); //label, value
        countries.put("Afghanistan", "  Afghanistan ");
        countries.put("Albania", "  Albania ");
        countries.put("Algeria", "  Algeria ");
        countries.put("Andorra", "  Andorra ");
        countries.put("Angola", "   Angola  ");
        countries.put("Antigua & Deps", "   Antigua & Deps  ");
        countries.put("Argentina", "    Argentina   ");
        countries.put("Armenia", "  Armenia ");
        countries.put("Australia", "    Australia   ");
        countries.put("Austria", "  Austria ");
        countries.put("Azerbaijan", "   Azerbaijan  ");
        countries.put("Bahamas", "  Bahamas ");
        countries.put("Bahrain", "  Bahrain ");
       countries.put("Tuvalu", "    Tuvalu  ");
        countries.put("Uganda", "   Uganda  ");
        countries.put("Ukraine", "  Ukraine ");
        countries.put("United Arab Emirates", " United Arab Emirates    ");
        countries.put("United Kingdom", "United Kingdom");
        countries.put("United States", "    United States   ");
        countries.put("Uruguay", "  Uruguay ");
        countries.put("Uzbekistan", "   Uzbekistan  ");
        countries.put("Vanuatu", "  Vanuatu ");
        countries.put("Vatican City", " Vatican City    ");
        countries.put("Venezuela", "    Venezuela   ");
        countries.put("Vietnam", "  Vietnam ");
        countries.put("Yemen", "    Yemen   ");
        countries.put("Zambia", "   Zambia  ");
        countries.put("Zimbabwe", " Zimbabwe    ");

    }

    public void countryLocaleCodeChanged(ValueChangeEvent e) {
        //assign new value to localeCode
        localeCode = e.getNewValue().toString();

    }

    public Map<String, String> getCountryInMap() {
        return this.countries;
    }

    public String getLocaleCode() {
        return localeCode;
    }

    public void setLocaleCode(String localeCode) {
        this.localeCode = localeCode;
    }

}

2 个答案:

答案 0 :(得分:1)

好吧,我不是Primefaces专家,所以这是一个简单的JSF-2示例,说明我是如何做到的:

<h:form>
  <h:outputLabel value="#{bundle.country}" for="Countries" />
  <h:selectOneMenu id="Countries" value="#{bean.addressToCreate.country}">
     <f:ajax render="postcodePanel" />
     <f:selectItems value="#{country.countryInMap}" />
  </h:selectOneMenu>

  <h:panelGroup id="postcodePanel">
    <h:outputLabel value="#{bundle.labelPostcode}" for="Postcode" />
    <h:inputText rendered="#{bean.addressToCreate.country == 'United Kingdom'}" 
        id="Postcode" 
        value="#{bean.addressToCreate.postcode}" 
        title="#{bundle.labelPostcode}" 
        required="true"
        requiredMessage="#{bundle.requiredPostcode}">
      <f:validator validatorId="PostcodeValidator" />
    </h:inputText>
    <h:message for="Postcode" />
  </h:panelGroup>
</h:form>

这里的主要内容是<f:ajax>标记,它会在change事件发生时自动触发AJAX请求。然后它告诉JSF重新呈现postcodePanel

答案 1 :(得分:0)

1.删除p:ajax组件。

2.在您的countryLocaleCodeChanged(ValueChangeEvent e)方法中添加以下代码,以执行视图更新工作。

RequestContext.getCurrentInstance.update("yourFormId:yourPanelId:postcodePanel(write the component's relative path to UIViewRoot)");

3.最后将required="True"替换为required="true"

4.可选地,您应该将渲染的属性添加到p:outputLabel,以便在不需要时隐藏邮政编码标签。