当至少填写一个字段时,根据需要验证一组字段 [英] Validate a group of fields as required when at least one of them is filled

查看:16
本文介绍了当至少填写一个字段时,根据需要验证一组字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个基本的注册屏幕(带有按钮注册记录屏幕)中有两个面板:

In a basic registration screen (with button register records the screen) there are two panels:

数据面板:

地址面板:

我可以仅通过完成数据面板来注册.没有必要填写地址面板.但是,如果至少填写了地址面板的一个字段,则同一面板中的所有其他字段都应该是必需的.

I can register by completing only the Data panel. It is not necessary to fill the Address panel. However, if at least one field of the Address panel is filled, then all other fields in the same panel should be required.

我怎样才能做到这一点?

How can I achieve this?

推荐答案

如果其他输入提交了非空值,您需要检查 required 属性.由于这会产生相当多的样板文件,这里有一个只有 3 个输入组件的启动示例.

You need to check in the required attribute if the other inputs have submitted a non-empty value. Since this can result in quite some boilerplate, here's a kickoff example with only 3 input components.

<h:form id="form">
    <h:inputText id="input1" value="#{bean.input1}" required="#{empty param['form:input2'] and empty param['form:input3']}" />
    <h:inputText id="input2" value="#{bean.input2}" required="#{empty param['form:input1'] and empty param['form:input3']}" />
    <h:inputText id="input3" value="#{bean.input3}" required="#{empty param['form:input1'] and empty param['form:input2']}" />
</h:form>

另一种方法是将组件绑定到视图并使用 UIInput#getValue()检查前面组件的值和UIInput#getSubmittedValue() 检查它们的下一个组件(组件即按照它们在组件树中出现的顺序进行处理).这样您就不需要对客户端 ID 进行硬编码.您只需要确保绑定名称不会与现有的托管 bean 名称冲突.

An alternative is to bind the components to the view and use UIInput#getValue() to check the value of the previous components and UIInput#getSubmittedValue() to check them for next components (components are namely processed in the order as they appear in the component tree). This way you don't need to hardcode client ID's. You only need to ensure that binding names doesn't conflict with existing managed bean names.

<h:inputText binding="#{input1}" value="#{bean.input1}" required="#{empty input2.submittedValue and empty input3.submittedValue}" />
<h:inputText binding="#{input2}" value="#{bean.input2}" required="#{empty input1.value and empty input3.submittedValue}" />
<h:inputText binding="#{input3}" value="#{bean.input3}" required="#{empty input1.value and empty input2.value}" />

你会明白,当你有越来越多的组件时,这会产生丑陋的样板.JSF 实用程序库 OmniFaces 有一个 用于确切目的的验证器.另请参阅现场演示.根据您的问题标签,您正在使用 OmniFaces,因此您应该已经设置好了:

You'll understand that this produces ugly boilerplate when you have more and more components. The JSF utility library OmniFaces has a <o:validateAllOrNone> validator for the exact purpose. See also the live demo. Based on your quesiton tags, you're using OmniFaces, so you should already be set with just this:

<o:validateAllOrNone components="input1 input2 input3" />
<h:inputText id="input1" value="#{bean.input1}" />
<h:inputText id="input2" value="#{bean.input2}" />
<h:inputText id="input3" value="#{bean.input3}" />

这篇关于当至少填写一个字段时,根据需要验证一组字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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