Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the validation message for a specific component

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

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.

like image 917
unpix Avatar asked Aug 30 '26 16:08

unpix


1 Answers

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}" />

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

  1. Implement a method that'll pull the detail from an available FacesMessage list, given a 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;
    }
    
  2. 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}" />
    
like image 108
kolossus Avatar answered Sep 02 '26 12:09

kolossus