I am adding a geolocation field to a simple visualforce page and controller extension which lets users set a few fields on their user record so I can use them in places using the $user
global variable
The page works fine, but as longitude needs to be from -180 to 180, I'm trying to add a bit of validation to ensure that the value is within bounds.
I'm doing so by adding some validation to the pagereference
method that updates the user
. It seems like the controller is evaluating the value from the initial query, and not the value that is coming from the visualforce page.
The reason I believe this is that When I click save, with a value of 999 for longitude, I get a system error for "value outside of valid range on numeric field." If the IF
statement was evaluating 999, it would be TRUE, and so the controller should just write to the debug log and do nothing.
Would appreciate guidance on the correct way to go about this. I tried to shorten the code snippets to just include the relevant bits of controller and visualforce page.
Controller (snippet)
public class UserControllerExtension {
public UserControllerExtension(ApexPages.StandardController stdController) {
}
public user u = [SELECT Id,Location_For_Distance_Reporting__latitude__s, Location_For_Distance_Reporting__longitude__s, Location_Units__c From USER Where Id =:UserInfo.GetuserId()];
public User getUser () {
return u;
}
public PageReference saveChanges() {
IF(math.abs ( u.Location_For_Distance_Reporting__longitude__s ) > 180){
system.debug('error - longitude out of bounds');
}
ELSE {
update u;
}
return null;
}
Visualforce Page (snippet)
fwiw i'm looking at switching the whole thing to use apex:panelgrid
, but got sidetracked by working on the field validation
<apex:page standardController="User" extensions="UserControllerExtension" sidebar="false" showHeader="false" showchat="false">
<apex:pageBlock>
<apex:pageBlockSection collapsible="false" columns="1" id="output">
<span class="sectionType">Location Reporting Values</span>
<apex:panelGrid style="width:62%; padding-top:5px; font-size:1em;" id="cSettings2" columns="2" columnClasses="colA,colB" rowClasses="rowA">
<apex:outputLabel value="Location Name" />
<apex:outputField value="{!User.Location_Name__c}" />
<apex:outputLabel value="Distance Units (mi/km)" />
<apex:outputField value="{!User.Location_Units__c}" />
<apex:outputLabel value="Latitude" />
<apex:outputField value="{!User.Location_For_Distance_Reporting__Latitude__s}" />
<apex:outputLabel value="Longitude" />
<apex:outputField value="{!User.Location_For_Distance_Reporting__Longitude__s}" />
</apex:panelGrid>
</apex:pageBlockSection>
<apex:pageBlockSection columns="1" collapsible="false">
<table style="width:85%; padding-top:5px;">
<apex:form>
<tr>
<td>
<span class="sectionType">Location Reporting Values</span>
</td>
</tr>
<tr>
<td class="inputs">
<apex:outputLabel value="Custom Location Name" styleClass="labelCol" />
</td>
<td class="inputs">
<apex:inputField value="{!User.Location_Name__c}" />
</td>
</tr>
<tr>
<td class="inputs">
<apex:outputLabel value="Units" styleClass="labelCol" />
</td>
<td class="inputs">
<apex:inputField value="{!User.Location_Units__c}" />
</td>
</tr>
<tr>
<td class="inputs">
<apex:outputLabel value="New Latitude" styleClass="labelCol" />
</td>
<td class="inputs">
<apex:inputField value="{!User.Location_For_Distance_Reporting__Latitude__s}" />
</td>
</tr>
<tr>
<td class="inputs">
<apex:outputLabel value="New Longitude" styleClass="labelCol" />
</td>
<td class="inputs">
<apex:inputfield value="{!User.Location_For_Distance_Reporting__Longitude__s}" />
</td>
</tr>
<tr>
<td></td>
<td>
<apex:commandButton action="{!saveChanges}" value="Update Values" />
</td>
</tr>
</apex:form>
</table>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
StandardController
reference instead of the one you queried for because of a naming conflict. Try renaminggetUser
to something else and then update your markup to reference the new property.pageMessages
tag to your markup. Add an error message to the page instead of debugging in your method. Make sure you rerender yourpageMessages
from yourcommandButton
. You'll probably get better performance though if you validate through javascript.