仅验证表单的特定部分,而不验证整个表单 [英] Validate only specific parts of the form instead of whole form

查看:103
本文介绍了仅验证表单的特定部分,而不验证整个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多组件的整体表格,其中包括p:tab

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

  • 我需要验证整个表单所需的消息,但我确实需要忽略p:tab必需的消息字段.
  • 如果我单击p:tab内的p:commandButton id = c2 ,则只需要验证p:tab内的必填消息字段即可.

什么是最好的解决方案?预先感谢.

解决方案

您似乎正在使用上帝形式"反模式.一切都放在一个<h:form>中.这是不良的设计/做法.最明智的方法是将字段和按钮以单独的形式放置,以使只有相关的字段和按钮处于自己的形式,这样表单提交才不必不必要地以其他形式提交/处理/转换/验证无关的数据. /p>

另请参见:


如果由于某些(奇怪的)设计限制而无法实现,那么至少还有另外两种方法:

  1. 如果使用的是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" />
    

    另请参见:了解PrimeFaces流程/更新和JSF f:ajax执行/render属性

  2. 如果您不使用ajax,或者希望使用非ajax后备,则只需检查required属性中已按下的按钮即可.通过在请求参数映射中检查按钮的客户端ID的存在,这很容易.

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

    (注意:c1c2不需要其他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}" ... />
    

I have a whole form with a lot of components including a p:tab

When I click on p:commandButton id=c1 to submit the whole form content:

  • I need to validate the whole form required messages but I do need to ignore the p:tab required messages fields.
  • If I click on p:commandButton id=c2 inside the p:tab, I need to validate only the required messages fields inside the p:tab.

What is the best solution for this ? Thanks in advance.

解决方案

You seem to be using the "God Form" anti-pattern. Everything is thrown together in a single <h:form>. This is a poor design/practice. Most sensible way would be to put the fields and buttons in separate forms so that only the related fields and buttons are in its own form so that the form submit doesn't unnecessarily submit/process/convert/validate irrelvant data in other forms.

See also:


If that's not possible due to some (strange?) design restrictions, then there are at least 2 other ways:

  1. If you're using ajax, then you can make use of the process attribute. It defaults to @form which would process the entire form. It accepts a space separated string of (relative) client IDs of input field which you'd like to process during submit.

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

    See also: Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes

  2. If you're not using ajax, or desire a non-ajax fallback, then just check in the required attribute which button has been pressed. This is easy by checking the presence of the button's client ID in the request parameter map.

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

    (note: no additional bean properties necessary for c1 or c2! the code is as-is)

    See also How to let validation depend on the pressed button?

    You can refactor this somewhat with a more self-documenting variable name:

    <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}" ... />
    

这篇关于仅验证表单的特定部分,而不验证整个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆