打开对话框验证主表单我不需要的内容

时间:2019-09-24 20:02:20

标签: jsf primefaces

如果单击添加按钮(例如“ CreateCategory1” ID)以添加一个新产品类别,则会收到“要求输入名称字段”验证消息。这应该打开一个对话框,在这里我可以输入新的类别名称,仅此而已。在这一点上,我不想验证主表单,因为我只想添加一个新产品类别,我将在以后使用。为什么对话框打开会验证表单?如何避免这种情况?

非常感谢您!

    <?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 xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui">

    <ui:composition >
        <h:outputScript library="js" name="warning.js"/>  
        <h:form id="ProductEditForm" target="_blank">
            <div class="ui-fluid">
                <p:panelGrid columns="4" columnClasses="ui-grid-col-6,ui-grid-col-2,ui-grid-col-2,ui-grid-col-2" layout="grid" styleClass="ui-panelgrid-blank" >
                    <p:outputPanel style="height: 70px" >
                        <p:breadCrumb model="#{breadCrumb.productMenuModel}" style="background: inherit; border: none" />           
                        <h2>#{productController.selected.name}</h2>
                    </p:outputPanel>
                    <p:commandButton style="height: 30px; background: #{userController.headerColor}"  icon="ui-icon-check" oncomplete="javascript:saved=true" action="#{productController.update}" actionListener="#{breadCrumb.navigateProduct()}" update=":center,:growl" value="#{bundle.Save}" />
                    <p:commandButton style="height: 30px" icon="ui-icon-close" oncomplete="javascript:saved=true"  action="#{productController.refreshSelected()}" actionListener="#{breadCrumb.navigateProduct()}" update=":center,:growl" value="#{bundle.Cancel}"/>
                    <p:commandButton style="height: 30px" icon="ui-icon-document" update="@([id$=ProductHistoryDateForm])" oncomplete="PF('ProuctHistoryDateDialog').show()" value="Print history" disabled="#{empty productController.selected}" />
                </p:panelGrid>
                <p:panelGrid columns="2" columnClasses="ui-grid-col-6,ui-grid-col-6" layout="grid" >
                    <p:panel>
                        <h3>Main info</h3>   
                        <p:panelGrid columns="2" columnClasses="ui-grid-col-3,ui-grid-col-9" layout="grid" styleClass="ui-panelgrid-blank">
                            <p:outputLabel value="#{bundle.EditProductLabel_name}" for="name" />
                            <p:inputText id="name" value="#{productController.selected.name}" title="#{bundle.EditProductTitle_name}" required="true" requiredMessage="#{bundle.EditProductRequiredMessage_name}"/>
                            <p:outputLabel value="#{bundle.EditProductLabel_originalName}"/>
                            <p:inputText id="originalName" value="#{productController.selected.originalName}"/>
                            <h:outputText value="#{bundle.ViewInventoryItemLabel_serialized}"/>
                            <p:selectBooleanCheckbox disabled="false" value="#{productController.selected.serialized}" />
                            <p:outputLabel value="#{bundle.EditProductLabel_ean}" for="ean" />
                            <p:inputText id="ean" value="#{productController.selected.ean}" title="#{bundle.EditProductTitle_ean}" />
                            <p:outputLabel value="#{bundle.EditProductLabel_itemNr}" for="itemNr" />
                            <p:inputText id="itemNr" value="#{productController.selected.itemNr}" title="#{bundle.EditProductTitle_itemNr}" />
                            ...
                        </p:panelGrid>
                        <h3>Sales</h3>
                        <p:panelGrid columns="2" columnClasses="ui-grid-col-3,ui-grid-col-9" layout="grid" styleClass="ui-panelgrid-blank">
                            <p:outputLabel for="defaultSupplier" value="#{bundle.EditProduct_defaultSupplier}"/>
                            <p:selectOneMenu id="defaultSupplier" filter="true" value="#{productController.selected.defSupplierPartner}" title="#{bundle.EditProduct_defaultSupplier}" effect="fold" editable="false">
                                <f:selectItem itemLabel="#{bundle.SelectOneMessage}" itemValue="#{null}" noSelectionOption="true" />
                                <f:selectItems value="#{partnerController.items}"
                                               var="partnerDataIdItem"
                                               itemValue="#{partnerDataIdItem}"
                                               itemLabel="#{partnerDataIdItem.name}"/>
                            </p:selectOneMenu>
                            <p:outputLabel value="#{bundle.EditProductLabel_purchaseDeliveryTime}" for="purchaseDeliveryTime" />
                            <p:inputText id="purchaseDeliveryTime" value="#{productController.selected.purchaseDeliveryTime}" title="#{bundle.EditProductTitle_purchaseDeliveryTime}" />
                            <p:outputLabel value="#{bundle.EditProductLabel_lastPurchase}" />
                            <p:inputText id="lastPurchase" value="#{productController.selected.lastPurchasePrice}" />
                            ...
                        </p:panelGrid>
                    </p:panel>
                    <p:panel id ="productGroups">
                        <h3>Product categories</h3>
                        <p:panelGrid columns="3" columnClasses="ui-grid-col-1,ui-grid-col-1,ui-grid-col-11" layout="grid" styleClass="ui-panelgrid-blank">

                            <p:commandButton id="CreateCategory1" actionListener="#{productCategoryController.prepareCreate(1)}" update="productGroups, ProductCategoryCreateForm" icon="ui-icon-plus" oncomplete="PF('ProductCategoryCreateDialog').show()"/>                            
                            <p:commandButton id="DeleteCategory1" disabled="#{productController.selected.productCategoryId1 == null}" update="category1" actionListener="#{productCategoryController.deleteProductCategory(productController.selected.productCategoryId1)}" icon="ui-icon-minus" />   

                            <p:selectOneMenu id="category1" value="#{productController.selected.productCategoryId1}" label="#{productController.selected.productCategoryId1.name}" effect="fold">
                                <f:selectItem itemLabel="#{bundle.SelectOneMessage}" itemValue="#{null}" noSelectionOption="true" />
                                <f:selectItems value="#{productCategoryController.getItemsByLevel(1)}" 
                                               var="item" 
                                               itemLabel="#{item.name}"/>
                                <p:ajax event="change" update="productGroups" listener="#{productController.changeCategory(1)}"/>
                            </p:selectOneMenu>
                            <p:commandButton id="CreateCategory2" rendered="#{productController.selected.productCategoryId1 != null}" actionListener="#{productCategoryController.prepareCreate(2)}" update="productGroups, ProductCategoryCreateForm" icon="ui-icon-plus" oncomplete="PF('ProductCategoryCreateDialog').show()"/>
                            <p:commandButton id="DeleteCategory2" disabled="#{productController.selected.productCategoryId2 == null}" update="category2" actionListener="#{productCategoryController.deleteProductCategory(productController.selected.productCategoryId2)}" icon="ui-icon-minus" />   

                            <p:selectOneMenu id="category2" rendered="#{productController.selected.productCategoryId1 != null}" label="#{productController.selected.productCategoryId2.name}" value="#{productController.selected.productCategoryId2}" effect="fold">
                                <f:selectItem itemLabel="#{bundle.SelectOneMessage}" itemValue="#{null}" noSelectionOption="true" />
                                <f:selectItems value="#{productCategoryController.getItemsByLevel(2)}" 
                                               var="item"                                                   
                                               itemLabel="#{item.name}"/>
                                <p:ajax event="change" update="productGroups" listener="#{productController.changeCategory(2)}"/>
                            </p:selectOneMenu> 
                            <p:commandButton id="CreateCategory3" rendered="#{productController.selected.productCategoryId2 != null}" actionListener="#{productCategoryController.prepareCreate(3)}" update="productGroups, ProductCategoryCreateForm" icon="ui-icon-plus" oncomplete="PF('ProductCategoryCreateDialog').show()"/>
                            <p:commandButton id="DeleteCategory3" disabled="#{productController.selected.productCategoryId3 == null}" update="category3" actionListener="#{productCategoryController.deleteProductCategory(productController.selected.productCategoryId3)}" icon="ui-icon-minus" />   

                            <p:selectOneMenu id="category3" label="#{productController.selected.productCategoryId3.name}" rendered="#{productController.selected.productCategoryId2 != null}" value="#{productController.selected.productCategoryId3}" effect="fold">
                                <f:selectItem itemLabel="#{bundle.SelectOneMessage}" itemValue="#{null}" noSelectionOption="true" />
                                <f:selectItems value="#{productCategoryController.getItemsByLevel(3)}" 
                                               var="item"                                                   
                                               itemLabel="#{item.name}"/>
                                <p:ajax event="change" update="productGroups" listener="#{productController.changeCategory(3)}"/>
                            </p:selectOneMenu> 
                            <p:commandButton id="CreateCategory4" rendered="#{productController.selected.productCategoryId3 != null}" actionListener="#{productCategoryController.prepareCreate(4)}" update="productGroups, ProductCategoryCreateForm" icon="ui-icon-plus" oncomplete="PF('ProductCategoryCreateDialog').show()"/> 
                            <p:commandButton id="DeleteCategory4" disabled="#{productController.selected.productCategoryId4 == null}" update="category4" actionListener="#{productCategoryController.deleteProductCategory(productController.selected.productCategoryId4)}" icon="ui-icon-minus" />   

                            <p:selectOneMenu id="category4" label="#{productController.selected.productCategoryId4.name}" rendered="#{productController.selected.productCategoryId3 != null}" value="#{productController.selected.productCategoryId4}" effect="fold">
                                <f:selectItem itemLabel="#{bundle.SelectOneMessage}" itemValue="#{null}" noSelectionOption="true" />
                                <f:selectItems value="#{productCategoryController.getItemsByLevel(4)}" 
                                               var="item"                                                   
                                               itemLabel="#{item.name}"/>
                                <p:ajax event="change" update="productGroups" listener="#{productController.changeCategory(4)}"/>
                            </p:selectOneMenu> 
                        </p:panelGrid>
                    </p:panel>
                </p:panelGrid>
            </div> 
        </h:form>
    </ui:composition>
</html>

1 个答案:

答案 0 :(得分:1)

这不是直接的答案,而是关于这个问题到底有什么问题的广泛的广泛评论,以及如何通过在最后给出一个简单答案的[mcve] ...来缩小实际问题的范围。 / p>

  • 删除组成
  • 删除h:outputScript
  • 删除所有样式
  • 删除结构div的{​​{1}}等
  • 从标签中删除“捆绑包”并将其设置为静态
  • 删除大部分输入(保留其中的一个以便使它仍能说明问题)
  • 删除大多数打开对话框的按钮,然后将其中一个(您所指的那个)留在其中,以便它仍然可以显示问题

那会是什么样子

panelGrid

您甚至可以通过使用普通<?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 xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:p="http://primefaces.org/ui"> <h:form id="ProductEditForm" target="_blank"> <p:commandButton id="CreateCategory1" actionListener="#{productCategoryController.prepareCreate(1)}" update="productGroups, ProductCategoryCreateForm" oncomplete="PF('ProductCategoryCreateDialog').show()"/> <p:selectOneMenu id="category1" value="#{productController.selected.productCategoryId1}" label="Category 1"> <f:selectItem itemLabel="Select one" itemValue="#{null}" noSelectionOption="true" /> <f:selectItems value="#{productCategoryController.getItemsByLevel(1)}" var="item" itemLabel="#{item.name}"/> <p:ajax event="change" update="productGroups" listener="#{productController.changeCategory(1)}"/> </p:selectOneMenu> <p:selectOneMenu id="category2" rendered="#{productController.selected.productCategoryId1 != null}" label="Category 2" value="#{productController.selected.productCategoryId2}" effect="fold"> <f:selectItem itemLabel="Select one" itemValue="#{null}" noSelectionOption="true" /> <f:selectItems value="#{productCategoryController.getItemsByLevel(2)}" var="item" itemLabel="#{item.name}"/> <p:ajax event="change" update="productGroups" listener="#{productController.changeCategory(2)}"/> </p:selectOneMenu> </h:form> </html> 而不是p:inputText来使其更简单。

p:selectOneMenu

如果问题仍然存在,则<?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 xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:p="http://primefaces.org/ui"> <h:form id="ProductEditForm" target="_blank"> <p:commandButton id="CreateCategory1" actionListener="#{productCategoryController.prepareCreate(1)}" update="productGroups, ProductCategoryCreateForm" oncomplete="PF('ProductCategoryCreateDialog').show()"/> <p:inputText id="category1" value="#{productController.selected.productCategoryId1}" label="Category 1"> <p:ajax event="change" update="productGroups" listener="#{productController.changeCategory(1)}"/> </p:inputText> <p:inputText id="category2" rendered="#{productController.selected.productCategoryId1 != null}" label="Category 2" value="#{productController.selected.productCategoryId2}"> <p:ajax event="change" update="productGroups" listener="#{productController.changeCategory(2)}"/> </p:inputText> </h:form> </html> h:inputText甚至f:ajaxh:commandButton(将oncomplete更改为onevent),从而减少了PrimeFaces的依赖(仍然会导致错误)。

但是现在我们来看看...打开对话框的oncomplete ...实际上与打开对话框有关吗?如果您在commandButton中使用简单的f:ajax“怎么办?会失败吗?100%确定会这么做,所以您的标题

打开对话框可以验证我不需要的主要表单

和您的问题

“为什么对话框打开会验证表单?”

都错了...如果完全删除oncomplete怎么办?还是失败了?是的...嗯...所以这与按下按钮有关。那么按钮有什么作用?它提交了一个表格...对其进行验证...如此有效地使您的问题变成了

“如何防止commandButton提交和验证我的整个表单”

您还可以通过首先问自己一个与标题相关的问题来删除oncomplete,从而来到这里:

  

“这与打开对话框有关?如果我不打开对话框该怎么办?我该在哪里做?在oncomplete中,如果我删除了该怎么办?” )。

如果您在搜索引擎中发布了新问题,并在其中添加了“ PrimeFaces JSF site:stackoverflow.com”,那么您会在阅读后发现:

PrimeFaces disable validation on cancel button

与注释中所述完全相同。可能还不够(您可能需要使用特定按钮向要提交的字段的oncomplete="alert('Hi there');属性中添加其他一些ID,但是接下来您将需要更详细地说明问题。

您当然也可以制作一张以上的表格,提交较小的部分

How to use <h:form> in JSF page? Single form? Multiple forms? Nested forms?