如何禁用对selectOneMenu用于所需的标记时,号码:AJAX事件=变化? [英] How to disable the required tag on a selectOneMenu when p:ajax event=change?
问题描述
下面是我的配置:
- PrimeFaces:4.0.4(精英)
- OmniFaces:1.6.3
- 在JSF:MyFaces的2.0.2
- 服务器:的WebSphere 8.5.0.2
有些code(我只张贴了更清晰的相关部分):
Some code (I only post the relevant part for better clarity):
<p:selectOneMenu value="#{viewModel.selectedContact}" required="true" converter="omnifaces.SelectItemsConverter">
<p:ajax event="change" listener="#{viewModel.updateContact}" update="area"/>
<f:selectItem itemValue="#{null}" itemLabel="#{msg.no_contact}" noSelectionOption="true" />
<f:selectItems value="#{viewModel.contacts}"/>
</p:selectOneMenu>
所以在这里,我们有一个简单的号码:selectOneMenu用于
包含的联系人列表
对象,以及一个不接触的选项。如果我想提交表单此字段是必需的。
So here, we have a simple p:selectOneMenu
that contains a list of Contact
objects as well as a "No Contact" option. This field is required if I want to submit the form.
当在列表中选择一个联系人,在 updateContact
方法被调用。这种方法将生成将显示在区域中的数据
由AJAX调用更新的页面部分
When a contact is selected in the list, the updateContact
method is called. This method will generate data that will be displayed in the area
section of the page updated by the AJAX call.
如果我选择了不接触的选项,验证错误被触发,因为此字段是必需的,因此 updateContact
方法不叫。我想方法被调用,因为我需要恢复一些数据则显示网页中的区域
部分。
If I select the "No Contact" option, a validation error is triggered as this field is required, so the updateContact
method is not called. I would like the method to be called as I would need to reset some data then hide the area
section of the page.
我用过程中曾试图=@这个
,立即=真正的
,但它不科技工作。随着立即=真正的
,选择联系方式
没有传递到 updateContact
方法。
I have tried using process="@this"
, immediate="true"
, but it doesn't work. With immediate="true"
, the selected Contact
is not passed to the updateContact
method.
那么,什么是选择一个空值时,绕过这一领域的验证的最佳方式?
So, what is the best way to bypass the validation of this field when selecting a null value?
推荐答案
让要求
属性检查是否调用命令按钮。您可以检查,通过按钮的客户端ID在请求参数映射的presence。
Let the required
attribute check if the command button is invoked. You can check that by the presence of the button's client ID in the request parameter map.
<p:selectOneMenu ... required="#{not empty param[save.clientId]}" />
...
<p:commandButton binding="#{save}" ... />
(注:code是原样,你并不需要一个bean属性这一点)的
如果不调用命令按钮(而是阿贾克斯变化监听器被调用),那么要求
属性将评估假
键,一切都会如你intented。
If the command button is not invoked (but instead the ajax change listener is invoked), then the required
attribute will evaluate false
and everything will go as you intented.
更新:根据该意见,你打算让他们看起来像要求在渲染响应所有的时间。所以,仅仅补充一点检查:
Update: as per the comments, you intend to make them appear like required during render response all time. So, just add that check:
<p:selectOneMenu ... required="#{not empty param[save.clientId] or facesContext.currentPhaseId.ordinal eq 6}" />
...
<p:commandButton binding="#{save}" ... />
这篇关于如何禁用对selectOneMenu用于所需的标记时,号码:AJAX事件=变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!