I mean one of BalusC's answers: JSF does not support cross-field validation, is there a workaround?
I follow the same path and exit with the code as shown below:
in .xhtml
<h:form id="form1"> <div> <p:messages globalOnly="true" display="text" /> <h:inputHidden value="true"> <f:validator validatorId="fooValidator" /> <f:attribute name="input1" value="#{input1}" /> <f:attribute name="input2" value="#{input2}" /> <f:attribute name="input3" value="#{input3}" /> </h:inputHidden> <h:panelGrid columns="3"> <h:outputText value="name 1: " /> <p:inputText binding="#{input1}" id="input11" value="#{testPage.input1}" /> <p:message for="input11" display="text"/> </h:panelGrid> <h:panelGrid columns="3"> <h:outputText value="name 2: " /> <p:inputText binding="#{input2}" id="input22" value="#{testPage.input2}" /> <p:message for="input22" display="text"/> </h:panelGrid> <h:panelGrid columns="3"> <h:outputText value="name 3: " /> <p:inputText binding="#{input3}" id="input33" value="#{testPage.input3}" /> <p:message for="input33" display="text"/> </h:panelGrid> <p:commandButton value="Submit" action="#{testPage.submitValidator}" update=":updateBody" /> </div> </h:form>
java class:
@FacesValidator(value="fooValidator") public class CustomValidator2 implements Validator { @Override public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { UIInput input1 = (UIInput) component.getAttributes().get("input1"); UIInput input2 = (UIInput) component.getAttributes().get("input2"); UIInput input3 = (UIInput) component.getAttributes().get("input3"); Object value1 = input1.getSubmittedValue(); Object value2 = input2.getSubmittedValue(); Object value3 = input3.getSubmittedValue(); if (value1.toString().isEmpty() && value2.toString().isEmpty() && value3.toString().isEmpty()) { String errorMsg = "fill in at least 1"; FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMsg, errorMsg); FacesContext.getCurrentInstance().addMessage("form1:input11", msg);
The code is working fine, but I am facing a problem. How to highlight the border of name1 inputText (or both name1 and name2 inputText) with red color, as JSF usually does when checking is not performed.
image as a link: http://img443.imageshack.us/img443/8106/errork.jpg
early
source share