PrimeFaces 8.0 / 10.0.RC1:处理来自其他对话框的表单时运行对话框表单验证

PrimeFaces 8.0 / 10.0.RC1: dialog form validation runs when processing form from other dialog

我正在打开 dialog1,我从中打开 dialog2,每个都有自己的表单,它们没有相互嵌套。

  1. 当我从 dialog1 表单内的按钮显示 dialog2 时,提交 dialog2 会验证 dialog1 表单 - 我不希望这样。

  2. 当我从 dialog1 表单外的按钮显示 dialog2 时,提交 dialog2 不会运行 dialog1 表单的验证。

我在 Primefaces 8.0 和 10.0.RC1 上测试过,它的行为是一样的。

为什么会这样?为什么从哪里显示对话框很重要? 谢谢。

示例代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<ui:composition
        xmlns:h="http://xmlns.jcp.org/jsf/html"
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
        xmlns:p="http://primefaces.org/ui"
        xmlns:f="http://java.sun.com/jsf/core"
        template="/template.xhtml">
    <ui:define name="content">
        <p:dialog appendTo="@(body)" widgetVar="dialog1" modal="true" header="dialog1"
                  showEffect="clip" closeOnEscape="true"
                  hideEffect="clip" responsive="true" width="700px" focus=":submitButtonForm:submitButton"
                  height="500">

            <p:remoteCommand name="showDialog2" action="PF('dialog2').show();"/>
            <h:form id="form1">
                <p:outputPanel>
                    <p:outputLabel for="required1" value="dialog1value"/>
                    <p:inputText id="required1" required="true" value="#{test.dialog1value}"/>

                    <p:outputLabel for="dialog2value" value="dialog2value"/>
                    <p:inputText id="dialog2value" required="true" value="#{test.dialog2value}"/>
                </p:outputPanel>
                <p:commandButton value="show dialog2 from within form1 of dialog1 (dialog2 Submit then validates form1)"
                                 onclick="PF('dialog2').show();"/>
            </h:form>

            <p:commandButton value="show dialog 2 from body of dialog1 (dialog2 Submit then DOES NOT validate form1)"
                             onclick="PF('dialog2').show();"/>

            <f:facet name="footer">
                <h:form id="submitButtonForm">
                    <p:commandButton partialSubmit="true"
                                     id="submitButton" value="Submit" actionListener="#{test.dialog1Submit}"
                                     update=":form1" process=":submitButtonForm,:form1"
                                     style="float:right;margin-bottom: 20px;"/>
                </h:form>
            </f:facet>
        </p:dialog>

        <p:dialog appendTo="@(body)" widgetVar="dialog2" modal="true" header="dialog2"
                  showEffect="clip" closeOnEscape="true"
                  hideEffect="clip" responsive="true" width="700px" focus=":submitButtonForm:submitButton"
                  height="500">

            <h:form id="form2">
                <p:outputPanel>
                    <p:outputLabel for="required2" value="dialog2value"/>
                    <p:inputText id="required2" required="true" value="#{test.dialog2value}"/>
                </p:outputPanel>
            </h:form>

            <f:facet name="footer">
                <h:form id="submitButtonForm2">
                    <p:commandButton partialSubmit="true"
                                     id="submitButton2" value="Submit" actionListener="#{test.dialog2Submit}"
                                     update=":form2,:form1" process=":submitButtonForm2,:form2"
                                     style="float:right;margin-bottom: 20px;"/>
                </h:form>
            </f:facet>
        </p:dialog>

        <p:commandButton value="show dialog 1" oncomplete="PF('dialog1').show();"/>
    </ui:define>
</ui:composition>
</html>

import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Component;

import javax.faces.view.ViewScoped;

@Component
@ViewScoped
public class Test {

    @Getter
    @Setter
    private String dialog1value, dialog2value;

    public void dialog1Submit() {
        System.out.println("dialog1 submitted");
    }

    public void dialog2Submit() {
        System.out.println("dialog2 submitted");
    }
}

将命令按钮更改为 return false 以阻止它提交 AJAX 表单...

<p:commandButton 
   value="show dialog 2 from body of dialog1 (dialog2 Submit then DOES NOT validate form1)"                             
   onclick="PF('dialog2').show();return false;"/>