Here is my configuration:
这是我的配置:
- PrimeFaces: 4.0.4 (elite)
- OmniFaces: 1.6.3
- JSF: MyFaces 2.0.2
- Server: WebSphere 8.5.0.2
PrimeFaces:4.0.4(精英)
JSF:MyFaces 2.0.2
服务器:WebSphere 8.5.0.2
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>
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.
所以在这里,我们有一个简单的p:selectOneMenu,它包含一个Contact对象列表以及一个“No Contact”选项。如果我想提交表单,则需要此字段。
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方法。此方法将生成将显示在由AJAX调用更新的页面的区域部分中的数据。
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.
我尝试过使用process =“@ this”,immediate =“true”,但它不起作用。使用immediate =“true”,选定的Contact不会传递给updateContact方法。
So, what is the best way to bypass the validation of this field when selecting a null value?
那么,在选择空值时绕过此字段验证的最佳方法是什么?
1 个解决方案
#1
8
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.
让required属性检查是否调用了命令按钮。您可以通过请求参数映射中存在按钮的客户端ID来检查它。
<p:selectOneMenu ... required="#{not empty param[save.clientId]}" />
...
<p:commandButton binding="#{save}" ... />
(note: code is as-is, you don't need a bean property for this)
(注意:代码是原样,你不需要bean属性)
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.
如果未调用命令按钮(而是调用ajax更改侦听器),则必需属性将评估为false,并且所有内容都将按照您的意图进行。
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}" ... />
#1
8
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.
让required属性检查是否调用了命令按钮。您可以通过请求参数映射中存在按钮的客户端ID来检查它。
<p:selectOneMenu ... required="#{not empty param[save.clientId]}" />
...
<p:commandButton binding="#{save}" ... />
(note: code is as-is, you don't need a bean property for this)
(注意:代码是原样,你不需要bean属性)
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.
如果未调用命令按钮(而是调用ajax更改侦听器),则必需属性将评估为false,并且所有内容都将按照您的意图进行。
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}" ... />