JSF:检查是否存在< h:message for ="id"/>的更好方法. [英] JSF : Better way to check for existence of <h:message for="id"/>

查看:151
本文介绍了JSF:检查是否存在< h:message for ="id"/>的更好方法.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种格式,其中需要在输入元素下方显示验证错误消息.需要通过在错误消息和输入文本周围显示错误提示框来突出显示该错误.

I have a form in which validation error message needs to be displayed below the input elements. The error needs to be highlighted by showing an error bubble around the error message and the input text.

要实现这一点,我需要检查单个元素是否存在h:messages. 我可以按照以下步骤检查是否存在全局错误消息

To achieve this, I need to check for the existence of h:messages for individual elements. I am able to check for the existence of global error messages as follows

<h:panelGroup rendered="#{not empty facesContext.messages}"> 
</h:panelGroup>

如何检查特定的客户ID(例如名字).所以像

How I can check the same for specific client id (say first name). So something like

faceContent.messages("creditCardNo")

我目前有一个解决方案是创建一个自定义解析器,但想知道是否有更好的解决方案.

A solution I have currently is to create a custom resolver but was wondering if there is a better solution.

推荐答案

需要通过在错误消息周围显示错误提示框来突出显示错误...

只需将<h:message>与样式类一起使用,即可在其中定义所需的样式.

Just use <h:message> with a styleclass wherein you define the desired style.

<h:inputText id="foo" />
<h:message for="foo" styleClass="error" />

使用

.error {
    border: 1px solid red;
    background: pink;
}


...以及输入文本.

只需检查 UIInput#isValid() true.从JSF 2.0开始,当前组件可通过隐式EL变量#{component}获得.

Just check if UIInput#isValid() is true. Since JSF 2.0 the current component is available by implicit EL variable #{component}.

<h:inputText styleClass="#{!component.valid ? 'error' : 'none'}" />


关于关于检查是否存在特定客户ID消息的实际问题,那么您可能会发现


As to the actual question about checking if there's a message for a certain client ID, then you may find this answer interesting. But I don't think that this is applicable in your particular case.

更新:根据评论,您似乎想对包含组件而不是单个组件进行样式设置.在这种情况下,请执行以下操作:

Update: as per the comments, you seem to want to style the containing component instead of the invididual components. In that case, do as follows:

<h:panelGroup styleClass="#{!foo.valid ? 'error' : 'none'}">
    <h:inputText id="foo" binding="#{foo}" />
    <h:message for="foo" />
</h:panelGroup>

这篇关于JSF:检查是否存在&lt; h:message for ="id"/&gt;的更好方法.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆