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
Abhilash Mishra 13Abhilash Mishra 13 

How to get Form data from visual force page as an instance of inner class

Hi I want to get all the fields data as and instace to inner class. is it possible?

I have written this code here

VF page
<apex:page controller="mycontroller">
   <apex:form>
      <apex:pageblock>
        <apex:pageblocksection>
           <apex:pageblocksectionitem>
                <apex:outputlabel for="fname"> First Name</apex:outputlabel>       
                <apex:inputtext value="{!info.firstname}" id="fname"/>
         </apex:pageblocksectionitem>
          <apex:pageblocksectionitem>
                <apex:outputlabel for="lname">Last Name</apex:outputlabel>
               <apex:inputftext value="{!info.lastname}" id="lname"/>
        <apex:pageblocksectionitem>
     </apex:pageblocksection>
     <apex:commandbutton action="{!savedata}" value="submit"/>   
</apex:pageblock>
   
</apex:form>
</apex:page>

Here is the controller :
public with sharing class mycontoller{

public info {
   get{
      if(info==null)
      return new information();
   return info;  
}
set;
}
 

 public information {
    public firstname {get;set;}
   public lastname {get;set;}

   }

}

above code doesnt seems to be working.

Can someone point out the issue?  need Help
Regards
Abhilash Mishra
Best Answer chosen by Abhilash Mishra 13
Abhilash Mishra 13Abhilash Mishra 13

Did It By modifying apex class to :

simple custom getter and setter methods:

public with sharing class mycontoller{

private information info;

public information getinfo() {
   if(information==null)
    information=new information();
return information; 
}

public void setinfo(information value){
   info=value;
}

 

 public information {
    public firstname {get;set;}
   public lastname {get;set;}

   }

}

All Answers

santanu boralsantanu boral
You can change the code as following:

Visualforce
<apex:page controller="myController">
   <apex:form >
      <apex:pageblock >
        <apex:pageblocksection >
           <apex:pageblocksectionitem >
                <apex:outputlabel for="fname"> First Name</apex:outputlabel>       
                <apex:inputText value="{!firstName}" id="fname"/>
             </apex:pageblocksectionitem>
          <apex:pageblocksectionitem >
                <apex:outputlabel for="lname">Last Name</apex:outputlabel>
               <apex:inputText value="{!lastName}" id="lname"/>
        </apex:pageblocksectionitem>
     </apex:pageblocksection>
       <apex:commandbutton action="{!savedata}" value="submit"/>  
</apex:pageblock>   
</apex:form>
</apex:page>

Controller:
 
public with sharing class mycontoller
{


public information info {
   get{
      if(info==null)
      return new information();
   return info;  
}
set;
}

public String firstName {
        get
        {
            return info.firstname;
        }
        set;
        }

public String lastName {
        get
        {
            return info.lastname;
        }
        set;
        }
        
 private class information 
 {
    public String firstname {get;set;}
    public String lastname {get;set;}
 }

}

If you are happy with this answer, please mark.
Abhilash Mishra 13Abhilash Mishra 13
Hi Shantanu
Thanks !!!!

But this is not what I want to do?  My objective is to minimize the variale declaration?

I actually have 4 field   2 for buyers name and 2 for sellers. and I was kind of hoping by using inner class I might be able to get them by reference of the innerclass.  

like this.
<apex:page controller="mycontroller">
   <apex:form>
      <apex:pageblock>
        <apex:pageblocksection>
           <apex:pageblocksectionitem>
                <apex:outputlabel for="fname"> First Name</apex:outputlabel>       
                <apex:inputtext value="{!info1.firstname}" id="fname1"/>
         </apex:pageblocksectionitem>
          <apex:pageblocksectionitem>
                <apex:outputlabel for="lname">Last Name</apex:outputlabel>
               <apex:inputftext value="{!info1.lastname}" id="lname1"/>
        <apex:pageblocksectionitem>
     </apex:pageblocksection>
        <apex:pageblocksection>
           <apex:pageblocksectionitem>
                <apex:outputlabel for="fname"> First Name</apex:outputlabel>       
                <apex:inputtext value="{!info2.firstname}" id="fname2"/>
         </apex:pageblocksectionitem>
          <apex:pageblocksectionitem>
                <apex:outputlabel for="lname">Last Name</apex:outputlabel>
               <apex:inputftext value="{!info2.lastname}" id="lname2"/>
        <apex:pageblocksectionitem>
     </apex:pageblocksection>
     <apex:commandbutton action="{!savedata}" value="submit"/>   
</apex:pageblock>
   
</apex:form>
</apex:page>

I want to achieve this  declaring Firstname and last name only once as an innerclass and user that class as a wrapper.

Regards
Abhilash Mishra
 

 
Abhilash Mishra 13Abhilash Mishra 13

Did It By modifying apex class to :

simple custom getter and setter methods:

public with sharing class mycontoller{

private information info;

public information getinfo() {
   if(information==null)
    information=new information();
return information; 
}

public void setinfo(information value){
   info=value;
}

 

 public information {
    public firstname {get;set;}
   public lastname {get;set;}

   }

}
This was selected as the best answer