如何验证是否输入了多个输入字段中的至少一个?

use*_*427 4 validation jsf

我有一个包含3个字段的表单,并提交按钮.单击按钮时,如果没有在3个字段中输入,则抛出验证消息.如果输入3个字段中的任何一个,则使用数据表处理数据并将结果显示在同一页面中.我可以为一个字段抛出验证消息,但不能为2个字段或更多字段抛出验证消息.这是我的代码.同样,如果我有一个需要传递的长值,并验证我该怎么做,因为长值无法验证为isEmpty()isNull().

这是我的代码,我想将它与多个字段一起使用,并使用具有长值的字段进行验证.

<h:inputText id="userName" value="#{user.userName}" required="true"
    requiredMessage="Please enter username" />

<h:inputText id="empId" value="#{user.empId}" required="true"
    requiredMessage="Please enter Employee Id" />

<h:inputText id="acctNm" value="#{user.acctNm}" required="true" 
    requiredMessage="Please enter Employee Id" />
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 6

只需让required每个字段的属性检查是否存在其他字段的提交值.提交的值#{param}在客户端ID作为键的参数映射中可用.

这是一个启动示例:

<h:form id="form">
    <h:inputText id="field1" ... required="#{empty param['form:field2'] and empty param['form:field3']}" />
    <h:inputText id="field2" ... required="#{empty param['form:field1'] and empty param['form:field3']}" />
    <h:inputText id="field3" ... required="#{empty param['form:field1'] and empty param['form:field2']}" />
</h:form>
Run Code Online (Sandbox Code Playgroud)

随着字段数量的增加,它变得更加难看.

或者,您可以使用OmniFaces <o:validateOneOrMore>:

<h:form id="form">
    <h:inputText id="field1" ... />
    <h:inputText id="field2" ... />
    <h:inputText id="field3" ... />

    <o:validateOneOrMore id="oneOrMore" components="field1 field2 field3" />
    <h:message for="oneOrMore" />
</h:form>
Run Code Online (Sandbox Code Playgroud)

请注意,在操作方法中执行验证是错误的设计.你应该使用标准JSF验证设施,此如requiered,validator,<f:validator>和/或<f:validateXxx>.