apex:component rendered using apex:dynamicComponent – how to call controller method?

bit of an odd one…

I have a VF page:

    <apex:page showHeader="true" sidebar="true" controller="myController" >
        <apex:dynamicComponent componentValue="{!myComponent}" />
    </apex:page>

This works fine, content from myComponent displays perfectly in the parent VF page.

Here’s my component markup:

<apex:component controller="myComponentController" >
    <apex:form>
    <apex:pageBlock title="My Block" mode="edit" id="theBlock">
        <apex:pageBlockButtons location="top" id="theButtons">
            <apex:commandButton value="Save" action="{!doSave}"/>
            <apex:commandButton value="Cancel" action="{!doCancel}"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>

The problem is I am unable to call any methods inside myComponentController.cls. Here’s what one of those methods looks like:

    public void doSave () {
        system.debug('Entered doSave!');
        // Do some stuff
    }

My debug statement never appears in the logs, nor do any method entry lines for doSave() (same with doCancel()).

Any ideas what might be the problem here? TIA

Update: when explicitly embedded, i.e:

<c:myComponent />

The method is called as expected & I see the debug output in my logs.

Answer

Not sure if this will help, but here’s the code I just used in my sandbox trying to replicate your issue, and it actually worked as expected. Try it out, see if it will work on your end.

This should be pretty much 1:1 to what you were describing. If this works, you can start building upon it to see where it all goes wrong.

MyDynamicComponentController.apxc

public class MyDynamicComponentController {
    public void doSave () {
        system.debug('Entered doSave!');     
    }

    public void doCancel () {
        system.debug('Entered doCancel!');
    }
}

MyDynamicComponent.vfc

<apex:component controller="MyDynamicComponentController" >
    <apex:form >
        <apex:pageBlock title="My Block" mode="edit" id="theBlock">
            <apex:pageBlockButtons location="top" id="theButtons">
                <apex:commandButton value="Save" action="{!doSave}"/>
                <apex:commandButton value="Cancel" action="{!doCancel}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:component>

MyController.apxc

public class MyController {

    public Component.MyDynamicComponent getMyComponent() {
        Component.MyDynamicComponent myComp = new Component.MyDynamicComponent();
        return myComp;
    }
}

MyDynamicPage.vfp

<apex:page showHeader="true" sidebar="true" controller="MyController" >
   <apex:dynamicComponent componentValue="{!myComponent}" />
</apex:page>

Attribution
Source : Link , Question Author : Davin Casey , Answer Author : smukov

Leave a Comment