简体   繁体   English

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

[英]JSF : Better way to check for existence of <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. 需要通过在错误消息和输入文本周围显示错误气泡来突出显示错误。

To achieve this, I need to check for the existence of h:messages for individual elements. 为此,我需要检查单个元素的h:消息是否存在。 I am able to check for the existence of global error messages as follows 我能够检查是否存在全局错误消息,如下所示

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

How I can check the same for specific client id (say first name). 我如何检查相同的特定客户端ID(比如名字)。 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. 我目前的解决方案是创建一个自定义解析器,但想知道是否有更好的解决方案。

The error needs to be highlighted by showing an error bubble around the error message ... 需要通过在错误消息周围显示错误气泡来突出显示错误...

Just use <h:message> with a styleclass wherein you define the desired style. 只需使用带有样式的<h:message> ,即可定义所需的样式。

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

with

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

...and the input text. ...和输入文本。

Just check if UIInput#isValid() is true . 只需检查UIInput#isValid()是否为true Since JSF 2.0 the current component is available by implicit EL variable #{component} . 从JSF 2.0开始,当前组件可通过隐式EL变量#{component}

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

As to the actual question about checking if there's a message for a certain client ID, then you may find this answer interesting. 至于关于检查某个客户端ID是否有消息的实际问题,那么您可能会发现这个答案很有趣。 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. 更新 :根据评论,您似乎想要设置包含组件的样式而不是invididual组件。 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>

I use bootstrap and also needed this... solved it by creating a Custom component wrapper. 我使用bootstrap并且还需要这个...通过创建Custom组件包装器来解决它。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM