1

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 Answer 1

2

(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>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .