获取特定组件的验证消息 [英] Get the validation message for a specific component

查看:81
本文介绍了获取特定组件的验证消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<h:inputText id="myInputText"
             title="The text from validation message here"
             style="#{component.valid? '' : 'border-color:red'}"
             validator="#{MyBean.validate}"
             required="true"
             requiredMessage="required"
             value="#{MyBean.value} />
<p:message for="myInputText" display="text"/>

由于我想自定义在inputText组件中查找失败的验证,并且我知道可以知道该组件是否已成功验证,因此我想知道它是否可行以及如何获得验证消息,以便将其显示为我的inputText组件的标题.

Since I want to custom the looking for a failed validation in an inputText compoment and I know that it is possible to know whether the component was successfully validated or not, I would like to know if it is viable and how I can get the validation message, in order to display it as the tittle of my inputText component.

推荐答案

您要计划的问题是,单个组件可以将多个消息排队.那你要怎么办出于演示目的,您可以使用

The problem you will have with what you're planning is that a single component can have more than one message queued. What are you going to do then? For demonstration purposes, you can use

<h:inputText id="myInputText"
             title="#{facesContext.getMessageList('myInputText').get(0)}"
             style="#{component.valid ? '' : 'border-color:red'}"
             validator="#{MyBean.validate}"
             required="true"
             requiredMessage="required"
             value="#{MyBean.value}" />

您应该将逻辑移到辅助bean中:

EDIT : You should just move the logic into your backing bean:

  1. 实施一种方法,该方法将从给定的clientId

public String getComponentMessageDetail(String clientId) {
    String detail = null;
    FacesContext ctxt = FacesContext.getCurrentInstance();
    List<FacesMessage> componentMessages = ctxt.getMessages(clientId);

    if (componentMessages != null && componentMessages.isEmpty() == false) {
        //returns the detail, from only the first message!
        detail = componentMessages.get(0).getDetail();
    }

    return detail;
}

  • 在您的视图中使用实用程序方法

  • Use the utility method in your view

     <h:inputText id="myInputText"
                  title="#{MyBean.getComponentMessageDetail('myInputText')}"
                  style="#{component.valid ? '' : 'border-color:red'}"
                  validator="#{MyBean.validate}"
                  required="true"
                  requiredMessage="required"
                  value="#{MyBean.value}" />
    

  • 这篇关于获取特定组件的验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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