3

I know standard Salesforce has component dependencies on certain components; I want to recreate that functionality. For example if I attempt to use <apex:commandButton> outside of a <apex:form> wrapper I get the following error:

Error: <apex:commandButton> must occur between <apex:form></apex:form> tags

The requirement is as follows:

<apex:form> 
    <apex:commandbutton/> // required inside apex:form
</apex:form>

I want to be able to create this same dependency for a custom component. Is this possible and if so how?

For example if I put <c:innerComponent> somewhere on the page, I want it to prevent compiling and provide an error that that tag must be within an <c:outerComponent> tag

Error: <c:innerComponent> must occur between <c:outerComponent></c:outerComponent> tags

Meaning if I want to use the innerComponent then the outerComponent is required

<c:outerComponent> 
    <c:innerComponent/> // required inside c:outerComponent
</c:outerComponent>
5
  • use <apex:form> only in vf page not in component.. so when you add commandbutton in cmp then it will inside vf apex:form
    – Ratan Paul
    Commented Mar 24, 2016 at 5:21
  • Xtremefaith.. Can you pls add pseudo code, how you are implementing?
    – Ratan Paul
    Commented Mar 24, 2016 at 5:30
  • I did already... see references to <c:innerComponent> and <c:outerComponent> Commented Mar 24, 2016 at 5:33
  • 1
    I don't mean this to sound facetious as I can't think of a way to solve this but you could rename innerComponent to innerComponent_UseWithinOuterComponent to help avoid issues
    – cropredy
    Commented Mar 24, 2016 at 15:03
  • That's not necessary. We have documented processes to avoid this, I was just hoping to create the same ability in my custom component that Salesforce has in its standard components. I think this helps with overall consistency Commented Mar 28, 2016 at 20:55

1 Answer 1

0

I don't think there's a way to that out-of-the-box. What you could try is to add an attribute linked the parent component controller.

You won't have a nice dependency message like the one you mentioned, but you do ensure that only components with the correct controller can call the child component.

Something like this:

public class ParentComponentController {
    public ParentComponentController controller { get { return this; } }

    public ParentComponentController() {
        //do your stuff or leave it empty
    }
}

Child Component:

<apex:component >

    <apex:attribute name="dependency" type="ParentComponentController" required="true" description="securitycheck"/>

</apex:component>

Parent Component:

<apex:component controller="ParentComponentController">
    <c:dependency="{!controller}" /> 
</apex:component>

P.s.: I couldn't save this on the IDE, but if try it on the browser ir works fine.

You must log in to answer this question.

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