[英]JSF selectOneMenu validation and ajax rendering
I have a validation on my selectOneMenu to prevent the user to select "None" item in the list. 我在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>
I have my panel below and a want to show it only when my item is not "None". 我的面板在下面,并且只想在我的项目不是“无”时才显示它。 So when I select "None", the message should render and the panel disappear. 因此,当我选择“无”时,消息应呈现,面板消失。
<h:panelGroup id="toRender">
<h:panelGrid rendered="#{bean.selectedValue == 0 ? false : true">
...
</h:panelGrid>
</h:panelGroup>
It's working without the validation but I can't make both work.. It's like if validation prevents rendering. 它没有验证就可以工作,但是我不能同时做这两个工作。就像验证阻止了渲染一样。
Any suggestion? 有什么建议吗? Thanks 谢谢
I guess that bean.selectedValue
is an Integer, so try this: 我猜bean.selectedValue
是一个整数,所以试试这个:
<f:selectItem itemValue="0" itemLabel="None" noSelectionOption="true"/>
noSelectionOption="true"
is selected, it will be handled as empty, so no value will be submitted.
并且,如果选择了带有noSelectionOption="true"
的项目,则它将被视为空,因此不会提交任何值。
When you are checking against bean.selectedValue == 0
, selectedValue
will never - except maybe initially - be 0, because when you select the item with value 0 it will not be submitted, as described above.
当您检查bean.selectedValue == 0
, selectedValue
将永远不会为0(可能最初除外)为0,因为如上所述,当您选择值为0的项目时,它将不会被提交。
bean.selectedValue == 0
can not work together.
因此,您是对的,您的验证和bean.selectedValue == 0
的检查不能一起使用。
I would recommend you to just remove the validation.
我建议您只删除验证。
I have a response, it's not the solution but it's another way to do this... 我有一个回应,这不是解决方案,但这是实现此目标的另一种方式...
<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>
And in the bean 然后在豆子里
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"));
}
}
}
This way the validation process is not launched and the verification is done in the bean. 这样,不会启动验证过程,而是在Bean中完成验证。
If someone find something better, let me know! 如果有人找到更好的东西,让我知道!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.