最近我将我的应用程序更新到了 JSF 2.1.7 和 PrimeFaces 3.4.2。当使用下面的对话框添加新组时,在保存新组之前,我收到“名称大小必须在 1 到 40 之间”验证错误。当我单击选择器的添加按钮时会发生这种情况。据我所知,显示此消息是因为验证失败。当我添加 immediate=true
时,不会出现验证错误到 p:命令按钮。我不知道是什么触发了验证。
<h:form id="formg" prependId="false">
<!-- messages -->
<p:growl id="msgsg" showDetail="true" />
<!-- data table -->
<ui:include src="/WEB-INF/flows/groupsTable.xhtml" />
<p:separator />
<!-- bottom tool bar -->
<ui:include src="/WEB-INF/flows/groupsToolBar.xhtml" />
<!-- preview, edit dialog -->
<ui:include src="/WEB-INF/flows/groupsDialog.xhtml" />
</h:form>
<p:dialog id="dialogg" header="#{groupsBean.dialogTitle}"
widgetVar="groupsDialog" dynamic="true" resizable="false" width="800"
height="600" showEffect="fade" hideEffect="fade" modal="true">
<p:ajax event="close" listener="#{groupsBean.refresh}"
immediate="true" update=":formg" global="false" process="@this" />
<p:tabView id="tabPicker">
<p:tab title="General">
<h:panelGrid id="displayg" columns="2">
<h:outputText value="#Group name*:" />
<p:inputText value="#{groupsBean.selectedGroup.name}" size="40"
readonly="#{!groupsBean.updatable}" maxlength="40" />
</h:panelGrid>
</p:tab>
<p:tab title="Members">
<ui:include src="/WEB-INF/custom/picker.xhtml">
... some params passed to picker
</ui:include>
</p:tab>
</p:tabView>
</p:dialog>
选择器类似于 <p:password>
它由两个 p:dataTable 组件和它们之间的 4 个按钮组成。这些按钮通过 h:panelGrid 分组在一起。按钮属性类似。这是按钮示例代码:
<p:outputPanel autoUpdate="true">
<p:commandButton actionListener="#{eval.evaluateAsMethod(pickerAdd)}"
update="source, target, #{messages}" immediate="true"
disabled="#{pickerSourceDisabled}"
icon="ui-icon ui-icon-arrowthick-1-s" />
</p:outputPanel>
source、target是两个数据表的id。 pickerAdd 作为参数传递,值为 groupsBean.picker.add
。这些表包含 FooDomain 对象。
public class FooDomain implements Serializable {
...
@NotNull
@Size(min = 1, max = 40)
@Column(name = "NAME")
private String name;
...
}
最佳答案
PrimeFaces <p:commandButton>
默认情况下处理整个表单(如 process="@form"
),因此默认情况下它会触发所有验证。您的验证错误来自 @Size
对属性(property)的限制。如果您只想处理按钮自己的操作,那么您应该添加 process="@this"
.
<p:commandButton ... process="@this" />
immediate="true"
也可以用来解决这个问题,但它的行为在幕后有些不同:整个表单仍然被处理,但该操作是在 APPLY_REQUEST_VALUES 阶段而不是 INVOKE_ACTION 阶段调用的。并且只有也具有 immediate="true"
的输入组件set 也将被处理,其他的将被跳过。
关于JSF/PrimeFaces 对话框中的 Javabean 验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14355197/