Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional style in JSF when validation has failed in general

Tags:

jsf

jsf-2

i have h:messages to display error messages, and there's a component that i want its style to change in case of validation error occurs (if any component has a validation error or any validation message is rendered then change the style of this specific component).

i know about the way to change the style if the component has validation errors: JSF : Better way to check for existence of <h:message for="id"/>

but i want a more general way, to change style if any component in the form is not valid, or in other words any validation message is rendered.

please advise how to accomplish that.

like image 484
Mahmoud Saleh Avatar asked Oct 04 '12 12:10

Mahmoud Saleh


1 Answers

You can use FacesContext#isValidationFailed() to check if validation has failed in general.

<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 can namely also be global/success messages which are been added in action method.

<h:outputText ... styleClass="#{not empty facesContext.messageList ? 'hasmessage' : 'nomessage'}" />
like image 166
BalusC Avatar answered Nov 07 '22 15:11

BalusC