Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

jsf 2 - Conditional style in JSF when validation has failed in general

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'}" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...