如何知道子组件是否不是有效的 Primefaces?

How to know if component child is not valid Primefaces?

我正在使用 Primefaces 5.0,当 <p:inputText> 无效时,我需要在元素 <h:panelGroup> 中添加 class。我有以下代码:

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

提前致谢。

您显然已经知道可以使用 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() 方法,您可以使用该方法通过搜索表达式查找子组件。

知道了这一点,下面是您可以实现此目标的方法:

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

另请参阅: