首页 文章

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

提问于
浏览
2

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

最初隐藏每个对话框 . 每个人都有自己的按钮来显示 . 当一个处于活动状态并单击保存按钮时,即使必填字段具有值,也会出现验证错误 . 验证错误来自非活动对话框面板 . 可能会让我知道我想做什么 .

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>

2 回答

  • 5

    一种方法可能是我设置了必需=“condation” .

    这将是笨拙的 . 只需将每个单独的表单放在其自己独立的 <h:form> 组件中,或使用 <p:commandButton>process 属性来标识您要处理的区域 .

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

    拥有“上帝”形式绝不是一种糟糕的做法 .

    另见:

  • 1

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

    • 切勿将 <p:dialog 放入任何其他组件内 . 把它放在某个地方,使它落在 <h:body 的正下方 .

    • 给每个 <p:dialog 它's own form. But, don' t将对话框放在表单中,将表单放在对话框中 . 它's not good to update the dialog itself, only it'的内容 . (我认为这也可以解决你的问题 . )

    例如

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

相关问题