1

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.

occoa
  • 25
  • 1
  • 7

1 Answers1

4

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:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452