JSF 2.0 only allows you to validate the input on one field, like check to see if it's a certain length. It doesn't allow you to have a form that says, "enter city and state, or enter just a zip code."
How have you gotten around this? I'm only interested in answers that involve the validation phase of JSF. I'm not interested in putting validation logic in Managed Beans.
Apache ExtVal was not mentioned here.
There are some cross validations in it (among other validations which might be useful):
https://cwiki.apache.org/confluence/display/EXTVAL/Property+Validation+Usage#PropertyValidationUsage-CrossValidation
The easiest custom approach I've seen and used as far is to create a
<h:inputHidden>
field with a<f:validator>
wherein you reference all involved components as<f:attribute>
. If you declare it before the to-be-validated components, then you can obtain the submitted values inside the validator byUIInput#getSubmittedValue()
.E.g.
(please note the
value="true"
on the hidden input; the actual value actually doesn't matter, but keep in mind that the validator won't necessarily be fired when it's null or empty, depending on the JSF version and configuration)with
If you declare the
<h:inputHidden>
after the to-be-validated components, then the values of the involved components are already converted and validated and you should obtain them byUIInput#getValue()
or maybeUIInput#getLocalValue()
(in case theUIInput
isn'tisValid()
) instead.See also:
Alternatively, you can use 3rd party tags/components for that. RichFaces for example has a
<rich:graphValidator>
tag for this, Seam3 has a<s:validateForm>
for this, and OmniFaces has several standard<o:validateXxx>
components for this which are all showcased here. OmniFaces uses a component based approach whereby the job is done inUIComponent#processValidators()
. It also allows customizing it in such way so that the above can be achieved as below:with
The only difference is that it returns a
boolean
and that the message should be specified asmessage
attribute in<o:validateMultiple>
.