0

Simple use case of passing a parent controller's instance to a custom VF component's attribute to allow for inter-controller communication

Component

<apex:component id="SomeComponent">
    <apex:attribute name="controller" type="SomeParentController" description="the parent (calling) controller"/>
</apex:component>

VF Page

<apex:page id="SomeVF" controller="SomeParentController">
    <c:SomeComponent controller="{!this}"/>
</apex:page>

Main VF Controller

public with sharing class SomeParentController {
    public SomeParentController getThis() {return this;}
}

When compiling the VF page - get this error on the line c:SomeComponent ... :

Wrong type for attribute . Expected SomeParentController, found String

1

(An hour of my life lost on this one ..hopefully, this will aid someone else..)

You can't name a VF's component attribute with name="controller"

The VF page compiler gets confused, it knows the component's attribute type is SomeParentController but at the same time it is expecting when encountering the presence of an attribute controller=... to look like what you would expect in apex:page controller="aStringThatIsAControllerName"

You can resolve the problem by using an attribute name other than controller

Component

<apex:component id="SomeComponent">
    <apex:attribute name="somethingOtherThanLiterallyController" type="SomeParentController" description="the parent (calling) controller"/>
</apex:component> 

VF Page

<apex:page id="SomeVF" controller="SomeParentController">
    <c:SomeComponent somethingOtherThanLiterallyController="{!this}"/>
</apex:page>

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.