+ Start a Discussion
Jon Mountjoy_Jon Mountjoy_ 

Visualforce components exhibit weird behaviour if attribute name is the same as the variable name in controller

After a lot of debugging, I figured out why a component of mine was behaving weirdly.  It turns out the setter wasn't being called to set the value from the passed attribute.  Moreover, it turns out it *only* doesn't work if the attribute defined in the component shares the same name as the variable in the controller.  This seems very weird - I see no such restriction in the documentation, and it compiles just fine - it just silently fails.

Am I doing something wrong? 

Thanks
Jon


Here's my code.

Here's my Apex class that the component uses:
public class jonTest {
  public Integer yikes = 5;
  public Integer getYikes() { return yikes; }
  public void setYikes(Integer oops) { 
     yikes = oops; 
     System.debug('******' + oops); 
  }
}


Not much here at all.  Note the attribute is called 'yikes'.

Now define a component:

<apex:component controller="jonTest">

  <apex:attribute name="um" 
     description="um" 
     type="Integer" 
     assignTo="{!yikes}"/>

</apex:component>

Note the attribute is called 'um'.
 
Now in my Visualforce page I have this:
 <c:foo um ="44"  />

 If you access the page with the log window open, you'll see that the setter, setYikes(), is indeed called. Everything works.

Now change the component definition code to read like this instead:

<apex:component controller="jonTest"> 

<apex:attribute name="yikes" 
description="um" 
type="Integer" 
assignTo="{!yikes}"/> 

</apex:component>

The only thing changed here is the name, to "yikes" - which now correlates with the variable name in the controller.
Now make the corresponding change in the Visualforce page:
   <c:foo yikes ="44"  />

 The result is no output in the debug window.  The setter isn't called at all. 

I'm on sandbox, cs2, if that makes any difference.

 

 

Ron HessRon Hess
I have heard of this, changing the name is a workaround

i think sandbox is on the summer release, however please check that your classes are set to API 13.0

if so, then we can file a case for this issue.

thanks
Jon Mountjoy_Jon Mountjoy_
Yep, my classes are 13
dchasmandchasman
Known bug that we're working on for a patch to Summer '08 (not sure of the exact patch time frame just yet).