I'm building a form in primefaces and need to run validation on a specific field of all items in a <p:dataList>
. Specifically I need to make sure there are only a maximum 3 different values in any number of items in the list.
<p:dataList value="#{myBean.myItems}" var="it"
id="myDataList" rowIndexVar="rowIndex">
...
<p:inputNumber value="#{it.fieldToValidate}">
<f:validator validatorId="myValidator" />
</p:inputNumber>
...
</p:dataList>
And the validator:
@FacesValidator(value = "myValidator")
public class MyValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
if (component == null)
return;
Set<BigDecimal> myValues = new HashSet<BigDecimal>();
//now add all values to the set, but how to get them?
if (myValues.size() > 3) {
throw new ValidatorException(new FacesMessage(
FacesMessage.SEVERITY_ERROR, "To many different values",
"There can be only three"));
}
}
}
I have read Validation across multiple fields in JSF/PrimeFaces and
JSF doesn't support cross-field validation, is there a workaround? and
How validate two password fields by ajax?
but they don't solve my problem because I don't know the contents of my data list beforehand.
I am using JSF-2.1.7
, Primefaces-6.0.4
and jdk-1.6
, ancient I know but it can't be helped...