[英]JSF selectOneMenu validation and ajax rendering
我在selectOneMenu上進行了驗證,以防止用戶在列表中選擇“無”項目。
<h:selectOneMenu id="selectMenu" value="#{bean.selectedValue}" required="true" requiredMessage="Please select an item">
<f:ajax render="toRender selectMenuMessage" listener="#{bean.onSelect}"/>
<f:selectItem itemLabel="None" noSelectionOption="true"/>
<f:selectItems value="#{bean.items}" var="item" itemValue="#{item.id}" itemLabel="#{item.label}"/>
</h:selectOneMenu>
我的面板在下面,並且只想在我的項目不是“無”時才顯示它。 因此,當我選擇“無”時,消息應呈現,面板消失。
<h:panelGroup id="toRender">
<h:panelGrid rendered="#{bean.selectedValue == 0 ? false : true">
...
</h:panelGrid>
</h:panelGroup>
它沒有驗證就可以工作,但是我不能同時做這兩個工作。就像驗證阻止了渲染一樣。
有什么建議嗎? 謝謝
我猜bean.selectedValue
是一個整數,所以試試這個:
<f:selectItem itemValue="0" itemLabel="None" noSelectionOption="true"/>
noSelectionOption="true"
的項目,則它將被視為空,因此不會提交任何值。
當您檢查bean.selectedValue == 0
, selectedValue
將永遠不會為0(可能最初除外)為0,因為如上所述,當您選擇值為0的項目時,它將不會被提交。
bean.selectedValue == 0
的檢查不能一起使用。
我建議您只刪除驗證。
我有一個回應,這不是解決方案,但這是實現此目標的另一種方式...
<h:selectOneMenu id="selectMenu" value="#{bean.selectedValue}">
<f:ajax render="toRender selectMenuMessage" listener="#{bean.onSelect}"/>
<f:selectItem itemLabel="None" noSelectionOption="true"/>
<f:selectItems value="#{bean.items}" var="item" itemValue="#{item.id}" itemLabel="#{item.label}"/>
</h:selectOneMenu>
然后在豆子里
public void onSelect() {
if(this.selectedValue != 0) {
// Do smthg here
} else {
// Display error message
UIComponent component = Outils.getFacesContext().getViewRoot().findComponent(":form:selectMenu");
if(component != null) {
Outils.getFacesContext().addMessage(component.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Test msg"));
}
}
}
這樣,不會啟動驗證過程,而是在Bean中完成驗證。
如果有人找到更好的東西,讓我知道!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.