jspjsfvalidationrendered-attribute

How to check if a JSF UIComponent is valid in a jsp file?


In a jsp file, is there a way to find a component on that page and check if that component is valid.

Say i have a an input field, and a panelGroup with complex html that should be showed if validation failed on the input field. What i'm really looking for is a EL expression for the rendered attribute on the panelGroup.

I tried playing around with a custom validator function, and here setting the panelGroups rendered value to true. But then it failed on required fields as the custom validator function was never run when this field was empty.

Right now i have avoided regular validation completely and use some rather ugly code to validate everything on submit. Then storing the result in a lot of instance booleans, which are used for rendered in the panelGroups.

Any help is much appreciated


Solution

  • You can make use of UIInput#isValid().

    Here's a kickoff example:

    <h:form>
        <h:inputText binding="#{input}" value="#{bean.input}" required="true" />
        <h:commandButton value="submit" action="#{bean.submit}" />
        <h:messages />
    </h:form>
    <h:outputText value="input is not valid!" rendered="#{!input.valid}" />
    

    Note that the name which you use in binding attribute should not clash with the names of any request scoped attributes/beans.