+ Start a Discussion
Jon KeenerJon Keener 

Accessing selected values of fields prior to page change or save

I'm trying to get a better understanding of how I might access values on a form that a user has selected prior to a page change or save. 
 
For example, in the code below, I have simple form with one picklist for user input.  I have also added a commandButton that re-renders an output panel below it.  In this sample, the output panel shows an output text area that shows whether the Picklist value is populated and what the value of the picklist currently is.  From what I have been able to tell, I don't have the ability to access the value of the user selected picklist value, because it has not been committed to the object variable, until a page change or save has occurred. 
 
Is there any plans to provide access to this type of information, similar to trigger.old and trigger.new in apex triggers?  I can definitely see value in being able to access the "trigger.old" values if you are editing an existing record, but interface decisions may need to be made based on newly selected values too.
 
As a side note:  I put a call to the Now() function in the Visualforce page to verify that the rerender was occurring.  I was concerned that since I didn't have an action on the button, that maybe the rerender was not running.  The time changes as expected when I press the button.  However, I noticed that the date format returned by Now() does not match the format stated in the current Visualforce documentation.  The documentation states that the Now function returns the date in the GMT timezone, and the example provided shows it including abbreviated day of the week and time down to the seconds.  When I run it, it is showing my current timezone along with not day of the week and time only down to the minutes.
 
 
 
Visualforce Code:
Code:
<apex:page id="step1" controller="testFieldChangeCheck" tabstyle="Contact">
<apex:form>
<apex:pageBlock title="test">
<apex:pageBlockSection title="Test 1">
        <apex:inputField id="picklist1" value="{!testing.LeadSource}" />
</apex:pageBlockSection>
<apex:pageBlockSection title="Test Button" id="TestButton" columns="1">
        <apex:commandButton id="showValues" value="Show Values" immediate="true" rerender="TestPanel">
</apex:commandButton>
</apex:pageBlockSection>

<!-- Test 1 - Test Results -->
<apex:outputPanel id="TestPanel">
<apex:pageBlockSection title="Test Results" id="TestResults" columns="1">
        <apex:outputText value="LeadSource Populated— {!Now()} : {!LeadSourcePopulated} {!testing.LeadSource}">
</apex:outputText>
</apex:pageBlockSection>
</apex:outputPanel>
<!-- End of Test 1 - Test Results -->
</apex:pageBlock>
</apex:form>
</apex:page>

 
Controller Code:
Code:
public class testFieldChangeCheck {
        Contact testing;

        public Contact getTesting() {
                if (testing == null) testing = new Contact();
                return testing;
        }

        public String getleadSourcePopulated() {
                if (testing.LeadSource == null) {return 'No';} else {return 'Yes';}
        }
}

 
dchasmandchasman
Jon,

"I put a call to the Now() function in..."

- I hit this one myself awhile back (and while using Now() for the very same reason), for now just wrap the Now() with a Text():

{!Text(Now())}

 to get it to the canonical form. What the doc is refering to is the result of Now() when used in a larger expression, e.g. Now() - 1, not the final datetime to string form. We have a bug to track either changing the doc or making the behavior match it or something else entirely (still thinking about this one).

"Is there any plans to provide access to this type of information..."

- I am unclear on if you want to solve this server side in apex code (e.g. validation logic can be added to your setter and you can throw a user defined exception to indicate failure - not exactly what you want I expect and we are working on improving on that) or using javascript (e.g. <apex:commandButton onclick="my javascript here"/> or any other js event. In the onclick case you can veto the postback which is very useful for client side validations.