I have a form with 3 fields, and submit button. when button clicked, if none is entered in 3 fields, throw validation message. If any one of the 3 fields are entered process
At least for iceFaces another approach to validate across multiple fields, especially when the validation is more complex than just "required", is to use application level validation inside your backing bean.
@see Custom Validator in Backing Beans
Regards
Rob
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>
.