I’m writing a custom component to show validation messages in popups, it works well but I have still a problem: I would like to display component labels instead of cliendIds in validation messages.
I already looked at this question and similars:
Removing the component Id from validation message when using message bundle in JSF
but I’m using jsf 1.1 and I get a compilation error if I try to set a value for attribute label or requiredMessage.
So I tried assigning a label to the component using an <outputLabel />:
<h:outputLabel for="phone" value="Phone number" />
<h:inputText id="phone" required="true" value="#{backingBean.phoneNumber}" />
with no effect.
Is there a simple way in jsf 1.1 to make label appear in validation messages instead of client id?
If not, how could I, given a component, retrieve its related label component to do the work in Java code?
Thanks.
I found a solution which does not require to modify existing JSF code.
HtmlMessagesRenderer, which is capable of use a component label instead of its idHtmlMessagesRenderer#findInputIdto retrieve the Id of the component with messagesHtmlMessagesRenderer#findInputLabelto retrieve the Label of the component with messagesBelow is an excerpt from the code of my component Render
encodeBeginmethod, in which I made two separated loops, the first for component messages the other for global messages.Note that
FacesContext#getClientIdsWithMessagesreturns alsonull, which is considered the clientId for global messages.Note that, because the component itself manages to retrieve and use the component label, if exists; this solution it only need to place a
<myCustomMessages />tag in JSF page code.Here is a reference to
HtmlMessagesRenderer#findInputLabelsource code, to take a look on how it works.