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.
Here's a kickoff example:
<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.
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>
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>
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…