6

In essence, I tried the following setup. I throw an exception in my example because it is simpler to determine if the setter is being called.

Component

<apex:component controller="MyController">
    <apex:attribute name="recordId" assignTo="{!controllingId}" type="Id"
        required="true" description="Id to set" />
</apex:component>

Controller

public class MyController
{
    public class VFException extends Exception { }
    public Id controllingId
    {
        get;
        set { throw new VFException(); }
    }
}

The above results in a script-thrown exception, as expected. But if I remove the getter or make it private, the setter no longer gets called. Why?

public Id controllingId { set { throw new VFException(); } }

The above allows for a page load. Yet this documentation has examples where properties are write-only or have a private setter.

4

While properties may be a combination of read-only, write-only, or read-write, the assignTo attribute will not work correctly without both a getter and setter. That's because assignTo is actually a two-way property. Most Visualforce expressions will only bind to an attribute that is both readable and writeable with at least public access.

This is covered in the documentation, which states:

assignTo A setter method that assigns the value of this attribute to a class variable in the associated custom component controller. If this attribute is used, getter and setter methods, or a property with get and set values, must be defined.

  • Just realized it is in the apex:attribute documentation...dumb mistake. If you merge in the description for assignTo, I will accept your answer. – Adrian Larson Dec 23 '15 at 18:01
  • 1
    @AdrianLarson It's amazing how often we miss stuff in the documentation. In fact, I didn't know this was in the documentation (I just knew it to be true). – sfdcfox Dec 23 '15 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.