在JSF/PrimeFaces中跨多个字段进行验证 [英] Validation across multiple fields in JSF/PrimeFaces

查看:59
本文介绍了在JSF/PrimeFaces中跨多个字段进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在多个字段之间进行验证,以确保仅在违反给定字段之一的情况下才需要验证.

I need to validate across multiple fields in such a way that validation should be violated only when one of the given fields is violated.

与跨字段验证不同,在跨字段验证中,一个字段的值取决于其余字段中一个或多个的值.

It is distinct from cross field validation in which a value of one field is dependent upon the value(s) of one or more of the rest of the fields.

考虑一个简单的情况.

<p:inputText id="txt1" value="#{testBean.txt1}" required="false" maxlength="45"/>
<p:inputText id="txt2" value="#{testBean.txt2}" required="false" maxlength="45"/>
<p:inputText id="txt3" value="#{testBean.txt3}" required="false" maxlength="45"/>

<p:commandButton id="btnSubmit" actionListener="#{testBean.insert}" 
                 icon="ui-icon-check" value="Save"/>

其中,仅当给定的三个文本字段之一留为空白时,才发生验证冲突.如果它们中的任何一个都填充了一个值,则应该对所有这些值进行验证.在这种情况下,不应违反验证.

In which, validation violation should be occurred only when one of the given three text fields is left blank. If anyone of them is filled with a value then, all should be validated. In which case, validation should not be violated.

如何进行这种情况? JSF/PrimeFaces是否提供某种方式以这种方式执行验证?

How to proceed with this scenario? Does JSF/PrimeFaces provide some way to perform validation in this way?

推荐答案

我很难满足您具体的功能要求,但我相信您正在寻找全有还是全无"的验证. IE.所有字段均应为空白,或所有字段均应填写. JSF实用程序库 OmniFaces 为此具有一个验证器,即

I have a hard time in wrapping my head around your concrete functional requirement, but I believe you're looking for "all or none" validation. I.e. either all fields should be blank, or all fields should be filled. JSF utility library OmniFaces has a validator for exactly this purpose, the <o:validateAllOrNone>.

这是您可以使用它的方式:

Here's how you could use it:

<p:inputText id="txt1" value="#{testBean.txt1}" maxlength="45" />
<p:inputText id="txt2" value="#{testBean.txt2}" maxlength="45" />
<p:inputText id="txt3" value="#{testBean.txt3}" maxlength="45" />
<o:validateAllOrNone components="txt1 txt2 txt3" />

这篇关于在JSF/PrimeFaces中跨多个字段进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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