通常验证失败时 JSF 中的条件样式 [英] Conditional style in JSF when validation has failed in general
问题描述
我有 h:messages 来显示错误消息,并且有一个组件,我希望它的样式在发生验证错误时更改(如果任何组件有验证错误或呈现任何验证消息,则更改此样式特定组件).
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).
如果组件有验证错误,我知道如何更改样式:JSF:检查 < 是否存在的更好方法;h:message for="id"/>
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.
请告知如何实现.
推荐答案
您可以使用 FacesContext#isValidationFailed()
用于检查一般情况下验证是否失败.
You can use FacesContext#isValidationFailed()
to check if validation has failed in general.
<h:outputText ... styleClass="#{facesContext.validationFailed ? 'fail' : 'success'}" />
或者,您可以使用 FacesContext#getMessageList()
检查是否有任何人脸消息.这不一定表示一般的验证失败,也可以是在 action 方法中添加的全局/成功消息.
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'}" />
这篇关于通常验证失败时 JSF 中的条件样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!