0

below is my code, I have created an VF Component/VFP/Controller

My question is: How can I save the value of the <apex:inputField in the VF Component

<apex:component>

  <apex:attribute name="fname" description="fn" type="myCustomObject__c" required="true" /> 
  <apex:attribute name="mname" description="mn" type="myCustomObject__c" required="true" /> 
  <apex:attribute name="lname" description="ln" type="myCustomObject__c" required="true" /> 

    <!-- more fields -->
    <apex:inputText value="{!fname.f_name__c}" id="fname"> </apex:inputText>
    <apex:inputText value="{!mname.m_name__c}" id="mname"> </apex:inputText>
    <apex:inputText value="{!lname.l_name__c}" id="lname"> </apex:inputText>

</apex:component>

VF Page:

<apex:page controller="NameInputTextPageController" >

  <apex:form>

  <c:NameInputTextComponent id="vfcom" 
      fname="{!firstname}" mname="{!middlename}" lname="{!lastname}"
  />

    <apex:commandButton action="{!saveme}" value="Save Data" />

  </apex:form>
</apex:page>

VF Page Controller:

public with sharing class NameInputTextPageController {
    public NameInputTextPageController() {}

    public PageReference saveme() {
        return null;
    }
}
6
  • Your controller is doesn't have the properties defined you are trying to reference...
    – Adrian Larson
    Mar 2 '16 at 21:04
  • correct! that's the part i'm trying to know how to handle it
    – Nick Kahn
    Mar 2 '16 at 21:05
  • saveMe isn't doing any DML
    – cropredy
    Mar 2 '16 at 21:17
  • @crop1645: i have just created action and off course needs to address how to grab the user entry data and save it and yes eventually it will be doing DML operation. sorry for the confusion
    – Nick Kahn
    Mar 2 '16 at 21:19
  • 1
    so firstName, middleName, and lastName are three instances of myCustomObject__c ? A separate object for each portion of someone's name?
    – cropredy
    Mar 2 '16 at 21:21
1

A couple things:

  • Add allowDml="true" to your <apex:component> tag.
  • You only need to pass one instance of your record, not three.
  • You can pass an ApexPage.Action and invoke that from your component.

Your markup could probably be simplified to something like:

<apex:page standardController="MyObject__c">
    <apex:form>
        <apex:commandButton value="Save" action="{!save}" />
        <c:nameInputs record="MyObject__c" />
    <apex:form>
</apex:page>

Then your component:

<apex:component allowDml="true">
    <apex:attribute name="record" type="MyObject__c" required="true"
        description="SObject record to edit" />\

    <apex:form>
        <apex:inputField value="{!record.f_name__c}" id="fname" />
        <apex:inputField value="{!record.m_name__c}" id="mname" />
        <apex:inputField value="{!record.l_name__c}" id="lname" />
    </apex:form>
</apex:component>
7
  • 1+ make sense how you simplified the page but confused where does the saving take place? i mean controller?
    – Nick Kahn
    Mar 2 '16 at 21:12
  • @AbuHamzah Just add it to a commandButton as you would with the save action normally. Updated my markup to demonstrate.
    – Adrian Larson
    Mar 2 '16 at 21:14
  • but if you look at my markup the save button is on the VF page not on the VF component.
    – Nick Kahn
    Mar 2 '16 at 21:16
  • @AbuHamzah Just use the save action on your page instead then. This structure will bind the values back.
    – Adrian Larson
    Mar 2 '16 at 21:27
  • I'm not getting the value so I have allowDml="true in the component and in the VF page I have public PageReference save() { system.debug(MyObject__c.f_name__c) } i get empty value in the debug
    – Nick Kahn
    Mar 2 '16 at 22:54

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.