更新时间:2022-03-24 06:31:35
只需让每个字段的required
属性检查其他字段的提交值是否存在.提交的值可在客户端客户ID作为键的参数映射#{param}
中获得.
Just let the required
attribute of each field check the presence of the submitted value of the other fields. The submitted value is available in the parameter map #{param}
by the client ID as key.
这是一个开球示例:
<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>
随着字段数量的增加,它只会变得更加丑陋.
It gets only more ugly as the amount of fields grows.
或者,您可以使用 OmniFaces
Alternatively, you can use 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>
请注意,以动作方法执行验证是错误的设计.为此,您应该使用标准的JSF验证工具,例如requiered
,validator
,<f:validator>
和/或<f:validateXxx>
.
Please note that performing validation in action method is bad design. You should use the standard JSF validation facilities for this such as requiered
, validator
, <f:validator>
and/or <f:validateXxx>
.