6

I refer to one of BalusC's answers: JSF doesn't support cross-field validation, is there a workaround?

I follow the same way, and come out with code as 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);
        //throw new ValidatorException(msg);
    }

    }
}

the code is working fine, but i face a problem. How to highlight border of name1 inputText(or both name1 and name2 inputText) with red color as usually done by JSF when validation fails.

image as reference: http://img443.imageshack.us/img443/8106/errork.jpg

thanks in advance

Community
  • 1
  • 1
heng heng
  • 693
  • 3
  • 13
  • 25

1 Answers1

8

Mark them invalid by UIInput#setValid(), passing false.

input1.setValid(false);
input2.setValid(false);
input3.setValid(false);

The borders are specific to PrimeFaces <p:inputText>, so you don't need to add any CSS boilerplate as suggested by the other answerer.

Note that this can also be achieved by OmniFaces <o:validateAll> tag without the need to homegrow a custom validator.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • hi @BalusC, how to use UIInput#setValid() in submit button action method, can give me a example? – heng heng Jan 18 '13 at 03:31
  • You can get the physical input component by its client ID from the view root by `UIViewRoot#findComponent()`. It's however recommend to perform the desired job in a validator. – BalusC Jan 18 '13 at 10:22
  • where to write the `component.setValid(false);` code. I am trying to put it into `public class EmailValidator implements Validator` the method `public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {` however `component` has no `setValid` method. – efirat Feb 08 '16 at 13:59
  • I am usind seam 2.3 and add custom EmailValidation... other form items has required validation. Now validation messages works however, there is no any highlight for input items which give error – efirat Feb 08 '16 at 14:15
  • sorry i have forgotten to say that, the code is throw it: `throw new ValidatorException(message);` actually, when I add email validator first input field has been redline alerted mode when I submit invalid mail adress. Then, somehow, it starts to seem normal either on invalid state or valid state – efirat Feb 08 '16 at 14:19
  • you are right! I have create a new question: http://stackoverflow.com/questions/35272053/seam-2-3-jsf-custom-email-validation-input-does-not-switch-error-state-even-giv Thank you – efirat Feb 08 '16 at 14:30