2

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

zoma
  • 35
  • 1
  • 6

1 Answers1

4

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
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you very much, this is precisely what i was looking for – zoma Jul 03 '10 at 05:46
  • You're welcome. By the way, are you aware that `` does less or more the same? – BalusC Jul 03 '10 at 06:06
  • 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 Aug 03 '10 at 13:21
  • 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 Aug 03 '10 at 13:31
  • 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 Aug 04 '10 at 08:31
  • 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 Aug 04 '10 at 12:24
  • Its JSF 1.1 under websphere portal 6.1, its ancient i know but a requirement – zoma Aug 05 '10 at 11:33
  • @BalusC If evaluating the validity within the inputText itself, do you still require the binding? EG conditionally adding aria-invalid attribute to h:inputText (via passthrough of course) – Mark W Jul 28 '21 at 09:44
  • 1
    @MarkW: it's not necessary in JSF 2.x (note that OP was using JSF 1.x). You can just use implicit EL object `#{component}` anywhere within context of component. See also e.g. https://stackoverflow.com/a/3140062 as an example. – BalusC Jul 28 '21 at 10:08