如何在需要时跳过对 JSF 2 中某些字段的验证 [英] How to skip validation on certain fields in JSF 2 when required
问题描述
我有两个 p:dialog.每一个都包含一些标记为必需的输入字段.一次只显示一个对话框,但是当我提交任何对话框时,JSF 还会验证未显示的对话框的输入字段.对于未显示的对话框,跳过 JSF 2 中的验证的最佳方法是什么.一种方法可能是我设置了 required="condation".但不知道这种情况可能是什么,它是否会起作用.
I have two p:dialog. Each one contains some input fields which are marked as required. Only one dialog is shown at a time but when I submit on any of the dialog, JSF also validates the input fields of the dialog which is not shown. What is the best approach to skip the validations in JSF 2 for the dialog which is not shown. One approach could be that I set the required="condation". But dont know what that condition could be and is it goin to work.
每个对话框最初都是隐藏的.每个都有自己的按钮来显示.当一个处于活动状态并且我单击保存按钮时,即使必填字段具有值,也会出现验证错误.验证错误来自非活动对话框面板.也许它可以让我对我正在尝试做的事情有所了解.
Each dialog is initially hidden. Each one has its own button to show. When one is active and I click the save button there is a validation error even when the required field has values. The validation error comes from the inactive dialog panel. May be it give some idea what i am trying to do.
更新
<p:dialog header="Edit Group" widgetVar="dialog_groupEdit" resizable="false"
width="300" showEffect="bounce" hideEffect="explode" modal="true" position="center">
<h:panelGrid id="panel_groupEdit" columns="2">
<h:outputText value="Group Name: "/>
<h:inputText id="input_gnameEdit" size="26" value="#{groupBean.selectionGroup.gname}" required="true" requiredMessage="Edit Group: Name is required."/>
<h:outputText value="Description:"/>
<h:inputTextarea rows="6" cols="23" value="#{groupBean.selectionGroup.description}"/>
<div></div>
<p:commandButton value="Save" action="#{groupBean.saveGroupChanges}" oncomplete="#{args.validationFailed ? '':'dialog_groupEdit.hide()'}"
update="panel_groups panel_groupEdit" style="float:right;"/>
<div></div>
<p:message for="input_gnameEdit"/>
</h:panelGrid>
</p:dialog>
<p:dialog header="New Group" widgetVar="dialog_groupCreate" resizable="false"
width="300" showEffect="bounce" hideEffect="explode" modal="true" position="center">
<h:panelGrid id="panel_groupCreate" columns="2">
<h:outputText value="Group Name: "/>
<h:inputText id="input_gnameCreate" size="26" value="#{groupBean.createGroup.gname}" required="true" requiredMessage="New Group: Name is reqired."/>
<h:outputText value="Description:"/>
<h:inputTextarea rows="6" cols="23" value="#{groupBean.createGroup.description}"/>
<div></div>
<p:commandButton value="Save" actionListener="#{groupBean.saveGroupCreate}" oncomplete="#{empty groupBean.createGroup.gname ? ' ':'dialog_groupCreate.hide()'}"
update="panel_groupCreate panel_groups" style="float:right;"/>
<div></div>
<p:message for="input_gnameCreate"/>
</h:panelGrid>
</p:dialog>
推荐答案
一种方法可能是我设置 required="condation".
这将是笨拙的.只需将每个单独的表单放在自己单独的
组件中,或者使用
的 process
属性> 确定您要处理的区域.
This is going to be clumsy. Just put each individual form in its own separate <h:form>
component, or use the process
attribute of the <p:commandButton>
to identify the region you'd like to process.
<p:commandButton process="panel_groupEdit" ... />
...
<p:commandButton process="panel_groupCreate" ... />
有一个上帝"形式在任何方面都是一种糟糕的做法.
Having a "God" form is in any way a poor practice.
这篇关于如何在需要时跳过对 JSF 2 中某些字段的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!