5

I have a h:form with several inputs and each of them got its own h:message and I'm looking for a way to show (using render or assigning some styleClass) some other element only when specific h:message is being shown (and hide when that h:message is not being displayed).

Here a code snippet

<li>
    <h:panelGroup id="current_password_wrapper">
        <p:password value="#{myBean.myCurrPass}" id="current_password" required="true"
            styleClass="required_field" />
    </h:panelGroup>
    <h:message for="current_password"/>
</li>
<li>
    <h:panelGroup id="new_password_wrapper">
        <p:password value="#{myBean.myNewPass}" id="new_password" required="true"/>
    </h:panelGroup>
    <h:message for="new_password"/>
    <h:commandLink value="my value"/>
</li>

I want to make the h:commandLink visible only when the <h:message for="new_password"/> is being displayed

So far I couldn't find anything...

Daniel
  • 36,833
  • 10
  • 119
  • 200
  • Do you want to create some kind of validation for input elements? – Christian Kuetbach Sep 16 '13 at 19:28
  • Is the message being shown on a validation error of a certain component? If so, which one? – BalusC Sep 16 '13 at 20:19
  • @BalusC , I populating `` with `FacesContext.getCurrentInstance().addMessage` from my bean for *new_password* id when the value in `myNewPass` is invalid (I also use that h:message for `required="true"` but it won't be a problem to add another h:message just for myNewPass validation error that is being checked in the bean) – Daniel Sep 16 '13 at 20:27

2 Answers2

4

If your environment supports EL 2.2, then you could check if FacesContext#getMessageList() isn't empty for the particular client ID.

<p:password binding="#{new_password}" ... />
<h:commandLink ... rendered="#{not empty facesContext.getMessageList(new_password.clientId)}" />

If the message is being shown as result of a validation error, then you could also just check the UIInput#isValid() state of the component associated with the message.

<p:password binding="#{new_password}" ... />
<h:commandLink ... rendered="#{not new_password.valid}" />

Note that manually adding a faces message to the context won't mark the input component invalid. Therefor either a true Validator should be used which throws a ValidatorException, or an explicit input.setValid(false) call has to be done programmatically.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks, used `input.setValid(false)` and `rendered="#{not new_password.valid}"`. But is there any way to display a certain element based on if some h:message is being visible or not ? I mean to use `binding` on ` – Daniel Sep 16 '13 at 20:52
  • @BalusC Sounds like a good idea for omnifaces! `` – Ben Nov 20 '13 at 10:09
  • 1
    @Ben: `` already does that: http://showcase.omnifaces.org/components/highlight – BalusC Nov 20 '13 at 10:20
  • @BalusC That just saved me a-lot of time. Thanks! – Ben Nov 20 '13 at 10:25
2

I think with the answer to this question you requirement can be archived:

How to number JSF error messages and attach number to invalid component

I think you can do something like this:

<h:outputText value="output text" rendered="#{bean.messageIndexes['form:input1'] > 0}" />
Community
  • 1
  • 1
Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79