You need to sign in to do that
Don't have an account?
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:
Not much here at all. Note the attribute is called 'yikes'.
Now define a component:
Note the attribute is called 'um'.
Now in my Visualforce page I have this:
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:
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:
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.
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'.
<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.
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