如何在需要时跳过 JSF 2 中某些字段的验证

发布于 2024-12-13 08:46:46 字数 2377 浏览 1 评论 0 原文

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

每个对话框最初都是隐藏的。每个按钮都有自己的显示按钮。当其中一个处于活动状态并且我单击“保存”按钮时,即使必填字段具有值,也会出现验证错误。验证错误来自非活动对话框面板。也许它能让我知道我正在尝试做什么。

更新

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很酷不放纵 2024-12-20 08:46:46

一种方法可能是我设置 required="condation"。

这会很笨拙。只需将每个单独的表单放入其自己单独的 组件中,或使用 process 属性即可> 确定您想要处理的区域。

<p:commandButton process="panel_groupEdit" ... />

...

<p:commandButton process="panel_groupCreate" ... />

拥有“上帝”形态无论如何都是一种糟糕的做法。

另请参阅:

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:

雨夜星沙 2024-12-20 08:46:46

当您使用 时,PF 论坛上建议的一些良好做法是 -

  1. 切勿将 放在任何其他组件中。将其放置在 正下方的某个位置。
  2. 为每个 提供其自己的形式。但是,不要将对话框放置在窗体内,而应将窗体放置在对话框内。更新对话框本身并不好,只更新其内容即可。 (我认为这也应该解决你的问题。)

例如

<p:dialog widgetVar="dlg" ...
    <h:form ...
        <h:panelGroup id="dlgContent" ... 

When you are using <p:dialog some of the good practices as advised on the PF forum are -

  1. Never put your <p:dialog inside any other component. Place it somewhere so that it falls right under the <h:body.
  2. Give every <p:dialog it's own form. But, don't place the dialog inside the form, place the form inside the dialog. It's not good to update the dialog itself, only it's content. (I think this should solve your issue also.)

e.g.

<p:dialog widgetVar="dlg" ...
    <h:form ...
        <h:panelGroup id="dlgContent" ... 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文