views:

26

answers:

1

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

+1  A: 

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.

BalusC
Thank you very much, this is precisely what i was looking for
zoma
You're welcome. By the way, are you aware that `<h:message>` does less or more the same?
BalusC
yes i have looked at message, however couldnt find a nice way of using it when the error message is more complex than just a string. Most of the error messages that have been specified to me have html lists in them or other html..
zoma
Ah yes, you want to display HTML in messages. Consider overriding the renderer. Also see http://balusc.blogspot.com/2010/07/using-html-in-jsf-messages.html
BalusC
Thats a relly cool solution, but refactoring at this stage is pain. I have run into a problem with the first solution you posted, #{!input.valid} seems to be false on the first page load aswell. Which means error messages are shown even though its not actually a postback, any ideas?
zoma
Sorry, I can't reproduce your problem. Which JSF impl/version are you using? I have tested on latest Mojarra 1.2 and 2.0.
BalusC
Its JSF 1.1 under websphere portal 6.1, its ancient i know but a requirement
zoma