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

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

问题描述

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

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

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

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

  • 我需要验证整个表单所需的消息,但我确实需要忽略 p:tab 所需的消息字段.
  • 如果我点击 p:tab 内的 p:commandButton id=c2,我只需要验证 p:tab 内的必填消息字段.
  • 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.

推荐答案

你似乎在使用God Form"反模式.一切都集中在一个 中.这是一个糟糕的设计/实践.最明智的方法是将字段和按钮放在单独的表单中,以便只有相关的字段和按钮在自己的表单中,这样表单提交就不会在其他表单中不必要地提交/处理/转换/验证无关数据.

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.

如果由于某些(奇怪的?)设计限制而无法做到这一点,那么至少还有其他两种方法:

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

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

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

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

如果您不使用 ajax,或者希望使用非 ajax 回退,那么只需检查已按下按钮的 required 属性.通过检查请求参数映射中按钮的客户端 ID 是否存在,这很容易.

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

(注意:c1c2 不需要额外的 bean 属性!代码保持原样)

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

另见 如何让验证依赖于按下的按钮?

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

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天全站免登陆