function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pintoo rajputpintoo rajput 

How to get input field value from page using Custom Controller

Dear all,


PAGE-
<apex:page controller="myComponentCon">
<apex:form >
<apex:outputPanel id="pan">

    <c:ComponentDisplayEmpName componentRegistration="Registration__c.FirstName__c">
     </c:ComponentDisplayEmpName>
     </apex:outputPanel>
     <apex:commandButton value="Add More" action="{!addmore}" reRender="pan"/>
     <apex:commandButton value="Submit" action="{!submit}"/>
    </apex:form>
</apex:page>




Custom Component-

<apex:component controller="myComponentCon">
    <apex:attribute name="var" type="String" description="The variable to represent a single Registrationin the iteration."/>
    <apex:repeat var="componentRegistration" value="{!reg}">
        <apex:componentBody >   
            <apex:outputText value="First Name"/>
            <apex:inputfield value="{!componentRegistration}"/>
        </apex:componentBody>
       
    </apex:repeat>
</apex:component>



Controller-


public class myComponentCon {

    public String getMyComponentCon() {
        return null;
    }

public List<Registration__c> reg {get;set;}
   
public myComponentCon ()
{
reg= new List<Registration__c>();
if(reg.size()==0)
{
Registration__c obj =new Registration__c();
reg.add(obj);
}
}
   
   
    public void addmore()
    {
    Registration__c obj =new Registration__c();
reg.add(obj);
system.debug(reg);
    }
   
    public void submit()
    {
    system.debug('reg'+reg);
   
   
    }
  
Best Answer chosen by pintoo rajput
Grazitti TeamGrazitti Team
Hi pintoo rajput,

The easiest approach to get the value from inputText using custom controller and save it, is by getting and setting the variable in custom controller.
And no need of Component in this case.

Refer to the following code to get input field value from page using Custom Controller.

APEX PAGE--

<apex:page controller="myComponentCon">
    <apex:form >
        <apex:pageBlock>
               <apex:outputLabel value="Enter your Name " for="firstname"></apex:outputLabel>
               <apex:inputText id="firstname" value="{!firstname}"/>
               <apex:commandButton action="{!submit}" value="Submit"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

CUSTOM CONTROLLER--
public class myComponentCon
{
    public String firstname {get;set;}
   
    public myComponentCon ()
    {
        firstname = '';
    }
    
    /*public void addmore()
    {
        Registration__c obj =new Registration__c();
        reg.add(obj);
        system.debug(reg);
    }*/
  
    public PageReference submit()
    {
        Registration__c r = new Registration__c();
        if (firstname == '')
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Please enter name'));
             return null;
        }
        try
        {
            r.firstname__c = firstname;
            insert r;
           
        }
        catch(Exception ex)
        {
            system.debug('exception');
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage() ));
            return null;
        }
        PageReference pageref = Page.RegisterPage;
        pageref.getParameters().put('thankYou', 'thanks');
        pageref.setRedirect(true);
        return pageref;  
    }
}


Please mark it as best answer if you find our answer helpful.
--
Regards,
Grazitti Team
Web: www.grazitti.com

All Answers

Grazitti TeamGrazitti Team
Hi pintoo rajput,

The easiest approach to get the value from inputText using custom controller and save it, is by getting and setting the variable in custom controller.
And no need of Component in this case.

Refer to the following code to get input field value from page using Custom Controller.

APEX PAGE--

<apex:page controller="myComponentCon">
    <apex:form >
        <apex:pageBlock>
               <apex:outputLabel value="Enter your Name " for="firstname"></apex:outputLabel>
               <apex:inputText id="firstname" value="{!firstname}"/>
               <apex:commandButton action="{!submit}" value="Submit"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

CUSTOM CONTROLLER--
public class myComponentCon
{
    public String firstname {get;set;}
   
    public myComponentCon ()
    {
        firstname = '';
    }
    
    /*public void addmore()
    {
        Registration__c obj =new Registration__c();
        reg.add(obj);
        system.debug(reg);
    }*/
  
    public PageReference submit()
    {
        Registration__c r = new Registration__c();
        if (firstname == '')
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Please enter name'));
             return null;
        }
        try
        {
            r.firstname__c = firstname;
            insert r;
           
        }
        catch(Exception ex)
        {
            system.debug('exception');
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage() ));
            return null;
        }
        PageReference pageref = Page.RegisterPage;
        pageref.getParameters().put('thankYou', 'thanks');
        pageref.setRedirect(true);
        return pageref;  
    }
}


Please mark it as best answer if you find our answer helpful.
--
Regards,
Grazitti Team
Web: www.grazitti.com
This was selected as the best answer
pintoo rajputpintoo rajput
thanks for your answer but i want same thing USING CUSTOM COMPONENT 
 
Thank You Grazitti Team