Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display JSF element only if specific h:message is being displayed

Tags:

jsf-2

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...

like image 902
Daniel Avatar asked Sep 16 '13 19:09

Daniel


2 Answers

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.

like image 90
BalusC Avatar answered Nov 15 '22 09:11

BalusC


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}" />
like image 36
Christian Kuetbach Avatar answered Nov 15 '22 10:11

Christian Kuetbach