JSF:检查<h:message for =“id”> </h:message>是否存在的更好方法

时间:2010-12-23 21:29:15

标签: jsf jsf-2

我有一个表单,其中需要在输入元素下方显示验证错误消息。需要通过在错误消息和输入文本周围显示错误气泡来突出显示错误。

为实现这一目标,我需要检查单个元素的h:消息是否存在。 我能够检查是否存在全局错误消息,如下所示

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

如何针对特定客户端ID(比如名字)检查相同内容。像

这样的东西
faceContent.messages("creditCardNo")

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

3 个答案:

答案 0 :(得分:21)

  

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

只需将<h:message>与样式相同,即可定义所需的样式。

<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}获得。

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

关于检查是否有针对某个客户端ID的消息的实际问题,您可能会发现this answer有趣。但我不认为这适用于您的具体情况。


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

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

答案 1 :(得分:0)

我使用bootstrap并且还需要这个...通过创建自定义组件包装器来解决它。

<composite:interface>
    <composite:attribute name="casoCustomFieldValue" required="true" />
    <composite:attribute name="casoOpen" default="true" />
</composite:interface>

<composite:implementation>

    <h:panelGroup styleClass="#{not empty facesContext.getMessageList(''.concat(cc.clientId).concat(':valortext')) ? 'form-group has-error' : 'form-group'}"
                  rendered="#{cc.attrs.casoCustomFieldValue.customField.fieldTypeId.fieldTypeId eq 'TEXT'}" >
        <p:inputText id="valortext" value="#{cc.attrs.casoCustomFieldValue.valor}" styleClass="form-control input-sm"
                 required="#{cc.attrs.casoCustomFieldValue.customField.required}" requiredMessage="Se necesita un valor." disabled="#{not cc.attrs.casoOpen}"/>
        <h:panelGroup rendered="#{not empty facesContext.getMessageList(''.concat(cc.clientId).concat(':valortext'))}" styleClass="help-block">
            <h:message for="valortext" />
        </h:panelGroup>
    </h:panelGroup>



</composite:implementation>

答案 2 :(得分:0)

.factory

如果在while循环中找到了您要查找的消息,则可以采取适当的措施