如何在需要时跳过JSF 2中某些字段的验证 [英] How to skip validation on certain fields in JSF 2 when required

查看:188
本文介绍了如何在需要时跳过JSF 2中某些字段的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个p:对话框。每个包含一些被标记为必需的输入字段。一次只显示一个对话框,但是当我在任何对话框上提交时,JSF还会验证对话框的未显示的输入字段。对于未显示的对话框,跳过JSF 2中的验证的最佳方法是什么。一种方法可能是我设置required =condation。但是不知道这种情况可能是什么,它是否会工作。



每个对话框最初都是隐藏的。每个人都有自己的按钮来显示。当一个是活动的,我单击保存按钮,即使在必需字段有值时也会出现验证错误。验证错误来自非活动对话框面板。可能会给我一些想法,我想做什么。



更新

 < 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:

这篇关于如何在需要时跳过JSF 2中某些字段的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆