如何在需要时跳过JSF 2中某些字段的验证 [英] How to skip validation on certain fields in JSF 2 when required
问题描述
每个对话框最初都是隐藏的。每个人都有自己的按钮来显示。当一个是活动的,我单击保存按钮,即使在必需字段有值时也会出现验证错误。验证错误来自非活动对话框面板。可能会给我一些想法,我想做什么。
更新
< p:dialog header =编辑组widgetVar =dialog_groupEditresizable =false
width =300showEffect =bouncehideEffect =explodemodal =trueposition = 中心 >
< h:panelGrid id =panel_groupEditcolumns =2>
< h:outputText value =组名:/>
< h:inputText id =input_gnameEditsize =26value =#{groupBean.selectionGroup.gname}required =truerequiredMessage =编辑组:名称是必需的。/>
< h:outputText value =说明:/>
< h:inputTextarea rows =6cols =23value =#{groupBean.selectionGroup.description}/>
< div>< / div>
< p:commandButton value =保存action =#{groupBean.saveGroupChanges}oncomplete =#{args.validationFailed?'':'dialog_groupEdit.hide()'}
update =panel_groups panel_groupEditstyle =float:right;/>
< div>< / div>
< p:message for =input_gnameEdit/>
< / h:panelGrid>
< / p:dialog>
< p:dialog header =新组widgetVar =dialog_groupCreateresizable =false
width =300showEffect =bouncehideEffect =explodemodal = trueposition =center>
< h:panelGrid id =panel_groupCreatecolumns =2>
< h:outputText value =组名:/>
< h:inputText id =input_gnameCreatesize =26value =#{groupBean.createGroup.gname}required =truerequiredMessage =New Group:Name is reqired。/>
< h:outputText value =说明:/>
< h:inputTextarea rows =6cols =23value =#{groupBean.createGroup.description}/>
< div>< / div>
< p:commandButton value =保存actionListener =#{groupBean.saveGroupCreate}oncomplete =#{empty groupBean.createGroup.gname?'':'dialog_groupCreate.hide()'}
update =panel_groupCreate panel_groupsstyle =float:right;/>
< div>< / div>
< p:message for =input_gnameCreate/>
< / h:panelGrid>
< / p:dialog>
方法可能是我设置了required =condation。
这将是笨拙的。只需将每个单独的表单放在单独的< h:form>
组件中,或者使用进程
< p:commandButton>
以标识您要处理的地区。
< p:commandButton process =panel_groupEdit... />
...
< p:commandButton process =panel_groupCreate... />
拥有上帝形式在任何方面都是一种不好的做法。
另请参见:
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.
UPDATE
<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>
One approach could be that I set the required="condation".
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.
See also:
- Understanding process and update attributes of PrimeFaces
- How to use <h:form> in JSF page? Single form? Multiple forms? Nested forms?
这篇关于如何在需要时跳过JSF 2中某些字段的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!