inputField databinding issue with VisualForce

Ben picture Ben · Dec 21, 2009 · Viewed 9.1k times · Source

I'm having strange results with a Visualforce page (yes, Salesforce.com is icky, I know). My issue is that I am trying to use the inputField to bind data on a custom sObject, but in my custom controller, it is not recognizing the user input data.

Here is the code snippet from the page:

<apex:pageBlockSection title="Enter New Fee" rendered="{!isRenderedFees}" >

    <apex:inputField value="{!workingFee.Fee_Type__c}"  required="True"/>
   <apex:inputField value="{!workingFee.Fee__c}"  required="True"/> 

    <apex:pageBlockSectionItem >
        <apex:CommandButton value="Save Fee" action="{!saveFee}"  immediate="true" />
        <apex:CommandButton value="Cancel" action="{!cancelFee}" immediate="true" />
    </apex:pageBlockSectionItem> 
 </apex:pageBlockSection>

and here is the code from the controller:

public Fee__c workingFee {get; set;}
 ....
public PageReference saveFee(){

    this.workingFee.Trade_Group__c = tradeGroup.id;
    try{
        System.debug('~~~~#~~#~~workingFee: '+workingFee.Fee_Type__c +'='+workingFee.Fee__c);
        upsert workingFee;
    }catch (System.Dmlexception e){
        ApexPages.addMessages(e);
        return null;
    }
    System.debug('~~~~#~~#~~workingFee: '+workingFee.Fee_Type__c +'='+workingFee.Fee__c);
    //savedFees.add(workingFee.clone());

    //re-render the page
    this.isRenderedFees = False;
    return null;
}

I've made sure the workingFee property is not null. Whenever I hit the "Save Fee" button after entering the values, it reloads the page and gives me the message "Error: Required fields are missing: [Fee__c]" (note, Fee__c here is a currency field -- it's not that it expects this to be an sObject, is it?)

The debug statement in the saveFee() method shows that workingFee's important fields are null, when I would expect them to have been assigned the values input by the user.

Answer

kiwipom picture kiwipom · Dec 21, 2009

I have had a whole heap of issues binding controls to a property exposed with the simple { get; set; } notation... The rest of your code will see the properties, but for some bizarre reason, your View won't (always) bind...

Try writing explicit get/set methods, like

private workingFee;
public Fee__c getWorkingFee() {
    return workingFee;
}

public void setWorkingFee(Fee__c value) {
    workingFee = value;
}

There is no logical reason why this should work any different to

public Fee__c workingFee { get; set; }

but in my experience, it sometimes does...

what did you say about it being icky? ;)