Skip to main content

Attribute 'for' of label component with id xxxx is not defined


I got the warning in the log file when I have used the tag <h:outputLabel> without attribute "for" in xhtml file. It was really polluting my server log files.

The logged information actually makes sense anyway! We could find an answer as the following:

"Having h:outputLabel without a "for" attribute is meaningless. If you are not attaching the label, you should be using h:outputText instead of h:outputLabel."

However, these solutions are not possible just for my situation. Instead of using h:outputText for only displaying text, my team has used h:outputLabel too many places. We were nearly in our release time (next day) so it is quite risky and takes much efforts if we try to correct it. Because the style (with CSS) is already done with h:ouputLabel. The alternative by adding attribute "for" the existing h:outputLabel is not reasonable either. I really need to find another solution.

Fortunately, I came across a way if I change to use p:outputLabel, the warning won't be logged! What?

I was just curious to know why; so then I decided to delve into the implementation of outputLabel of both Primefaces and Myfaces source code (at grepcode.com). I found the reason is:

In Myfaces (1.2.2), we can easily to find the line of code below in method "encodeBegin" in class "HtmlLabelRenderer". It is executed whether the attribute "for" is null.

log.warn("Attribute 'for' of label component with id " 
+ uiComponent.getClientId(facesContext)+" is not defined");

However, Primefaces (5.2) does not. In class "OutputLabelRenderer", it uses the default implementation of method "encodeBegin" from superclass without logging. Ahh! The method "encodeBegin" is just a hook!

Okay! The reason is clear. I saw that the only difference of HTML rendering between h:outputLabel and p:outputLabel is as below:

1. <h:ouputLabel> will be rendered  to become <label>
2. <p:outputLabel> will be rendered to become <label class="ui-outputlabel">

It should be an acceptable approach to fix the issue in my case. I just do a little bit more with style and I can achieve my goal without spending much efforts for testing whole the application again. Yeah!

Something like this:

1. XHTML

<h:panelGroup layout="block" styleClass="child-add-ons-container">

    <ul>

        <li>

            <h:panelGroup layout="block" styleClass="type-of-check">

                <p:outputLabel value="bonity"></p:outputLabel>

            </h:panelGroup>

        <li>

        ....

    <ul>

</h:panelGroup>

2. CSS: just make sure the style of p:outputLabel is the same with current style of h:outputLabel, for example:
.add-ons-compliance-check .type-of-check label.ui-outputlabel {

    font-style: normal; /*style of tag label in our app*/

    color: inherit;  /*style of tag label in our app*/

    font: inherit;  /*style of tag label in our app*/

}

References
[1]. http://stackoverflow.com/questions/12744264/attribute-for-of-label-error
[2]. http://docs.oracle.com/javaee/5/javaserverfaces/1.2/docs/tlddocs/h/outputLabel.html
[3]. http://grokbase.com/t/myfaces/users/143vdzqp11/how-to-disable-label-warnings
[4]. http://grepcode.com/file/repo1.maven.org/maven2/org.primefaces/primefaces/5.2/org/primefaces/component/outputlabel/OutputLabelRenderer.java?av=f
[5]. http://grepcode.com/file/repository.springsource.com/org.apache.myfaces/com.springsource.org.apache.myfaces/1.2.2/org/apache/myfaces/renderkit/html/HtmlLabelRenderer.java?av=f

Comments

Popular posts from this blog

[Snippet] CSS - Child element overlap parent

I searched from somewhere and found that a lot of people says a basic concept for implementing this feature looks like below: HTML code: <div id="parent">  <div id="child">  </div> </div> And, CSS: #parent{   position: relative;   overflow:hidden; } #child{   position: absolute;   top: -1;   right: -1px; } However, I had a lot of grand-parents in my case and the above code didn't work. Therefore, I needed an alternative. I presumed that my app uses Boostrap and AngularJs, maybe some CSS from them affects mine. I didn't know exactly the problem, but I believed when all CSS is loaded into my browser, I could completely handle it. www.tom-collinson.com I tried to create an example to investigated this problem by Fiddle . Accidentally, I just changed: position: parent; to position: static; for one of parents -> the problem is solved. Look at my code: <div class="modal-body dn-placeholder-parent-positi...

Selenium - Override javascript functions to ignore downloading process

I have got an issue with downloading process on IE 8. This browser blocks my automatic-download functionality on my app so that I could not work with my test case any more after that. In my case, I didn't care about the file is downloaded or not, I just focus on the result after downloading process finished successfully. Therefore, I found a solution to ignore this process so that I can work normally. I use Primefaces, here is the remote command to trigger the download process <p:remoteCommand name="cmdGenerateDocument" actionListener="#{logic.onGenerateDocument}" update="xrflDocumentCreationPanel" oncomplete="clickDownloadButton();"/> The following is my test case: @Test public void shouldUpdateStep6ToWarningIfStep1IsValidAfterFinished(){ MainPage mainPage = new MainPage(); waitForLoading(mainPage.getDriver()); EmployeeDetailPage empDetailPage = new EmployeeDetailPage(); waitForLoading(empDetailPage.getDriver()); e...

[Snippet] Generate a new unique "name" string from an existing list

Suppose that we have a list of employees. Everytime, we want to add new employee into this list, the name of the employee will be generated with the following rules: - the name of the new one is set to " [originalname] 1 " - in case the name already exist, " [originalname] 2 " is used, and so on. Here is my code snippet by Javascript: var employees =[ {id: 1, name: 'name'}, {id: 2, name: 'name 1'}, {id: 3, name: 'name 2'}, {id: 5, name: 'name 4'} ]; var commonUtils = { isExistName: function(_name, _collection, _prop) { for(var i = 0; i< _collection.length; i++){ if(_collection[i][_prop].localeCompare(_name)==0){ return true; } } return false; }, generateNewName: function(_name, _collection, _prop){ var i = 1; var searching = true; while (searching) { var newName = _name+ " " + i; if (!this.isExistName(newName, _collection, _pro...

Coding Exercise, Episode 1

I have received the following exercise from an interviewer, he didn't give the name of the problem. Honestly, I have no idea how to solve this problem even I have tried to read it three times before. Since I used to be a person who always tells myself "I am not the one good at algorithms", but giving up something too soon which I feel that I didn't spend enough effort to overcome is not my way. Then, I have sticked on it for 24 hours. According to the given image on the problem, I tried to get more clues by searching. Thanks to Google, I found a similar problem on Hackerrank (attached link below). My target here was trying my best to just understand the problem and was trying to solve it accordingly by the Editorial on Hackerrank. Due to this circumstance, it turns me to love solving algorithms from now on (laugh). Check it out! Problem You are given a very organized square of size N (1-based index) and a list of S commands The i th command will follow t...

Java 8 - Persistent data structure

The following is a series of posts about "functional programming in Java" which is the result of my understanding by reading the book " Java 8 in Action: Lambdas, Streams, and Functional-style Programming, by Alan Mycroft and Mario Fusco ". 1. Why functional programming? 2. Functional programming in Java 8 3. Java 8 - Using Functions as Values 4. Java 8 - Persistent data structure Persistent data structure is also known as a simple technique but it's very important. Its other names are functional data structure and immutable data structure. Why is it "persistent"? Their values persist and are isolated from changes happening elsewhere. That's it! This technique is described as below: If you need a data structure to represent the result of a computation, you should make a new one and not mutable an existing data structure. Destructive updates version public static A doSomething(A a){ a.setProp1("new value"); return...