问题
I want set a special error class to div block into my custom component for JSF. I want set errorClass to "error" if this field failed the validation.
<c:if test="${?????}">
<c:set var="errorClass" value="error" />
</c:if>
<div class="input ${errorClass}">
<label for="#{rich:clientId('input')}:input">#{cc.attrs.label}</label>
<h:inputText id="input" value="#{cc.attrs.value}"
<cc:insertChildren />
</h:inputText>
</div>
回答1:
You can use component.valid inside the style or styleClass attribute of your inputText:
<h:inputText value="#{cc.attrs.value}"
styleClass="#{component.valid ? '' : 'error'}" />
However, this won't work in your div since it is no jsf component. You could try component binding (from theory, not tested):
<div class="#{myComponent.valid ? '' : 'error'}">
<h:inputText id="input" value="#{cc.attrs.value}" binding="#{myComponent}">
<cc:insertChildren />
</h:inputText>
</div>
来源:https://stackoverflow.com/questions/11559460/how-to-get-validation-status-from-jsf-component