Conditional style in JSF

I have h: messages for displaying error messages, and there is a component that I want its style to change in case of a validation error (if any component has a validation error or some validation message, then change the style of this specific component )

I know how to change the style if the component has validation errors: JSF: the best way to check for the presence of <h: message for = "id" />

but I want a more general way to change the style if any component in the form is invalid, or in other words, any validation message is displayed.

please advise how to do this.

+8
jsf jsf-2
source share
1 answer

You can use FacesContext#isValidationFailed() to check if the whole check has passed.

 <h:outputText ... styleClass="#{facesContext.validationFailed ? 'fail' : 'success'}" /> 

Alternatively, you can use FacesContext#getMessageList() to check if there are any faces messages. This does not necessarily indicate a general validation failure; there may also be global / successful messages that have been added to the action method.

 <h:outputText ... styleClass="#{not empty facesContext.messageList ? 'hasmessage' : 'nomessage'}" /> 
+17
source share

All Articles