[SalesForce] How to call a standard controller action of a VF page from a VF component

I have a visual force page as follows which includes a component call Example one,

Component has a command button and I want to call the standard controller save action of VF page from the VF component.
Is this possible?

visualforce page

<apex:page standardController="Account">
  <apex:form>
    <apex:pageBlock title="My Content" mode="edit">
      <apex:pageBlockButtons>
        <apex:commandButton action="{!save}" value="Save"/>
      </apex:pageBlockButtons>
      <apex:pageBlockSection title="My Content Section" columns="2">
        <apex:inputField value="{!account.name}"/>
        <apex:inputField value="{!account.site}"/>
        <apex:inputField value="{!account.type}"/>
        <apex:inputField value="{!account.accountNumber}"/>
      </apex:pageBlockSection>
    </apex:pageBlock>
    <c:exampleOne/>
  </apex:form>
</apex:page>

Component "exampleOne"

<apex:component allowDML="true">
    <apex:commandButton action="{!save}" />
</apex:component> 

Note : I know we can use a separate controller class and use a custom save that would be my second option.

Best Answer

This should be doable by passing an argument of type ApexPages.Action to the component

<apex:page standardController="Account">
 <apex:form>
   <apex:pageBlock title="My Content" mode="edit">
   <apex:pageBlockButtons>
    <apex:commandButton action="{!save}" value="Save"/>
   </apex:pageBlockButtons>
   <apex:pageBlockSection title="My Content Section" columns="2">
    <apex:inputField value="{!account.name}"/>
    <apex:inputField value="{!account.site}"/>
    <apex:inputField value="{!account.type}"/>
    <apex:inputField value="{!account.accountNumber}"/>
  </apex:pageBlockSection>
 </apex:pageBlock>
 <c:exampleOne saveAction="{!save}"/>
 </apex:form>
</apex:page>

Component: exampleOne

<apex:component >
   <apex:attribute name="saveAction" type="ApexPages.Action" required="true"/>
   <apex:commandButton action="{!saveAction}" />
</apex:component> 

This example is covered in the excellent book Visualforce Development Cookbook by the inestimable Keir Bowden (aka @BobBuzzard)

Related Topic