Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms 仅验证表单的特定部分,而不是整个表单_Forms_Validation_Jsf_Primefaces - Fatal编程技术网

Forms 仅验证表单的特定部分,而不是整个表单

Forms 仅验证表单的特定部分,而不是整个表单,forms,validation,jsf,primefaces,Forms,Validation,Jsf,Primefaces,我有一个包含很多组件的完整表单,包括一个p:tab 当我点击p:commandButtonid=c1提交整个表单内容时: 我需要验证整个表单必需的消息,但我确实需要忽略p:tab必需的消息字段 如果单击p:tab内的p:commandButtonid=c2,则只需验证p:tab内所需的消息字段 这方面的最佳解决方案是什么?提前感谢。您似乎在使用“上帝形态”反模式。所有内容都集中在一个中。这是一个糟糕的设计/实践。最明智的方法是将字段和按钮放在单独的表单中,这样只有相关的字段和按钮才是自己的表

我有一个包含很多组件的完整表单,包括一个p:tab

当我点击p:commandButtonid=c1提交整个表单内容时:

  • 我需要验证整个表单必需的消息,但我确实需要忽略p:tab必需的消息字段
  • 如果单击p:tab内的p:commandButtonid=c2,则只需验证p:tab内所需的消息字段
这方面的最佳解决方案是什么?提前感谢。

您似乎在使用“上帝形态”反模式。所有内容都集中在一个
中。这是一个糟糕的设计/实践。最明智的方法是将字段和按钮放在单独的表单中,这样只有相关的字段和按钮才是自己的表单,这样表单提交就不会不必要地提交/处理/转换/验证其他表单中的不相关数据

另见:

如果由于某些(奇怪的?)设计限制而无法实现,则至少有两种其他方式:

  • 如果您使用的是ajax,那么就可以使用
    process
    属性。它默认为
    @form
    ,将处理整个表单。它接受一个空格分隔的输入字段的(相对)客户端ID字符串,您希望在提交过程中处理该字符串

    <p:inputText id="field1" ... required="true" />
    <p:inputText id="field2" ... required="true" />
    ...
    <p:inputText id="field3" ... required="true" />
    <p:inputText id="field4" ... required="true" />
    ...
    <p:commandButton id="c1" ... process="field1 field2" />
    ...
    <p:commandButton id="c2" ... process="field3 field4" />
    
    (注意:
    c1
    c2
    不需要额外的bean属性!代码保持原样)

    另见

    您可以使用一个更自我记录的变量名对其进行重构:

    <c:set var="c1ButtonPressed" value="#{not empty param[c1.clientId]}" />
    <c:set var="c2ButtonPressed" value="#{not empty param[c2.clientId]}" />
    ...
    <p:inputText id="field1" ... required="#{c1ButtonPressed}" />
    <p:inputText id="field2" ... required="#{c1ButtonPressed}" />
    ...
    <p:inputText id="field3" ... required="#{c2ButtonPressed}" />
    <p:inputText id="field4" ... required="#{c2ButtonPressed}" />
    ...
    <p:commandButton id="c1" binding="#{c1}" ... />
    ...
    <p:commandButton id="c2" binding="#{c2}" ... />
    
    
    ...
    ...
    ...
    ...
    

  • 仅供参考,您还可以处理包含要验证的控件的面板id-示例:

    <p:outputPanel id="thisPanel">
        <p:inputText id="field1" ... required="#{not empty param[c1.clientId]}" />
        <p:inputText id="field2" ... required="#{not empty param[c1.clientId]}" />
    
        <p:commandButton id="c2" ... process="thisPanel" />
    

    你的知识对我帮助很大。再次感谢您抽出时间。我将遵循最佳设计实践,并根据您的答案进行更改。
    <p:outputPanel id="thisPanel">
        <p:inputText id="field1" ... required="#{not empty param[c1.clientId]}" />
        <p:inputText id="field2" ... required="#{not empty param[c1.clientId]}" />
    
        <p:commandButton id="c2" ... process="thisPanel" />