occoa
occoa

Reputation: 25

How to know if component child is not valid Primefaces?

I'm using Primefaces 5.0 and I need to add class in element <h:panelGroup> when the <p:inputText> is not valid. I have the below code:

<h:panelGroup layout="block" styleClass="form-group #{ VALIDATION HERE ? '' : 'has-error'}" >                               
  <p:outputLabel for="txtUserId" value="ID:"/>
  <p:inputText styleClass="form-control" id="txtUserId" required="true" value="#{userAction.user.id}">                                  
      <f:ajax event="keyup" execute="@this" render="msgtxtUserId"/>
      <f:validateLength minimum="2" />                                  
  </p:inputText>
  <p:message for="txtUserId" id="msgTxtIdUsuario" />
</h:panelGroup>

Thank you in advance.

Upvotes: 1

Views: 843

Answers (1)

BalusC
BalusC

Reputation: 1109635

You apparently already know that you can use UIInput#isValid() to check whether an input component is valid or not and that you could in EL use #{component.valid} for this. You only don't yet seem to realize that #{component} actually refers to an instance of the UIComponent class. If you inspect its javadoc, then you should have noticed a findComponent() method which you could use to find a child component by a search expression.

Knowing this, here's how you can achieve this:

<h:panelGroup ... styleClass="#{component.findComponent('txtUserId').valid ? '' : 'has-error'}">
    <p:inputText id="txtUserId" ... />
    ...
</h:panelGroup>

See also:

Upvotes: 5

Related Questions