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
Amanda Byrne- Carolina Tiger RescueAmanda Byrne- Carolina Tiger Rescue 

How to fix required Apex:inputField that works on a pc, but not in Salesforce1?

Working on a ticket check-in Salesforce1 app. My relevant page code is:
<apex:inputField required="true" styleClass="detail" id="agreementField" value="{!Release_Form__c.DTCI_Agreement__c}">                                    
                <apex:param name="Type" value="{!Release_Form__c.DTCI_Agreement__c}" assignTo="{!agreement}"/>                            
            </apex:inputField>                       
            <br/>                           
            <apex:commandButton styleClass="btn" action="{!saveRelease}" value="Submit" title="Submit" immediate="false" />            
            <br/>

This works great in my sandbox if I'm on my PC, I cannot click the Submit button without checking the box, and if I try to do so, I get the appropriate error message. However, in our demo, we discovered that in Salesforce1, the user could submit and move on without checking the box.

Does anyone know why this might be happening and what workaround I could create? (I'm pretty rough at Javascipt, so if the workaround is Javascript, extra syntax would be awesome)

I can't imagine that this issue is by design.

 
 
 
Best Answer chosen by Amanda Byrne- Carolina Tiger Rescue
Amanda Byrne- Carolina Tiger RescueAmanda Byrne- Carolina Tiger Rescue
For whatever reason the javascript solution worked fine on my personal ipad, but failed to work on the organization ipads that would be using the app.

My ipad is a couple of years old, and was running 7.something.
The work ipads are Air2 and running iOS 8.4

But I found another, more elegant solution. Part of the reason I was having so many issues with the different apex solutions I'd been trying was because they were not re-querying my extension controller. 

I had a 'well, duh' moment when I was testing a different aspect of the app, and tried a simple if-then solution in the "saveRelease" function, so the code now looks like this (I was also unfamiliar with the 'addError' option):

Page Code:
<title2>I agree to the above: </title2>
                                                                
                <apex:inputField required="true" styleClass="detail" id="agreementField" value="{!Release_Form__c.DTCI_Agreement__c}">                                    
                    <apex:param name="Type" value="{!Release_Form__c.DTCI_Agreement__c}" assignTo="{!agreement}" />             
                </apex:inputField>
                                         
            <br/>                           
            <apex:commandButton styleClass="btn" action="{!saveRelease}" value="Submit" title="Submit" immediate="false" />            
            <br/>
and the extension Controller code looks like this:
//Release Form Save Button
    public PageReference saveRelease(){
        
        agreement = releaseForm.DTCI_Agreement__c;
        parentGuardianName = releaseForm.DTCI_Parent_or_Guardian__c;
        
         if(agreement == FALSE) {
            releaseForm.DTCI_Agreement__c.addError('Please check the agreement box to proceed.');
            return null;
         } else {
        
            Release_Form__c newRel = new Release_Form__c(DTCI_Attendee__c = attendeeId, 
                                                         DTCI_Parent_or_Guardian__c = parentGuardianName, 
                                                         DTCI_Agreement__c = agreement);        
            insert newRel;
                         
    }

It works beautifully.
 

All Answers

William TranWilliam Tran
Can you post the whole code including the controller showing the saveRelease method.  Is this an standardcontroller with extension or custom controller.

Is the issue that Saleforce1 is ignoring the field DTCI_Agreement__c a sa required field?

thx.
Terence_ChiuTerence_Chiu
Hi there, included below is an example of checking an inputField component's value by using $Component to reference vf components. As I do not know how the rest of your page is constructed I am giving an example of enforcing the account name field for a populated value when the Submit button is clicked using the onclick event and a javascript function that returns true if the field is populated and false if it is blank. If true is returned the action method is called as the form is submitted, otherwise the form does not and the action method is not called.
 
<apex:page standardController="Account" extensions="acctestctrl">
    <apex:form id="frm1">
        <apex:inputfield value="{!Account.Name}" id="nameid"/>
        <apex:commandButton value="Submit" action="{!saveaction}" onclick="return checkField();"/>
    </apex:form>
    
    <script>
        function checkField(){
            var name = document.getElementById('{!$Component.frm1.nameid}').value;
            if(name != null && name != ""){
                return true;
            }
            else{
                alert('Name needs to be filled out');
                return false;
            }
        }
    </script>
</apex:page>

Let me know if this helps. Here is the link with more details about $Component.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_access.htm
Amanda Byrne- Carolina Tiger RescueAmanda Byrne- Carolina Tiger Rescue
@Terence

Well, after fighting all day to try to get the Apex form validation, I tried your Javascript. (really not a js fan)

It worked!  Now I just have to figure out if you can style an alert box...

Thanks! If anyone comes up with an apex method, I'd still like to know.
 
<script>

        function checkField(){
            alert('function works!');

            if(document.getElementById('{!$Component.formRelease.agreementField}').checked){
                return true;
            }
            else{
                alert('Please check this box if  you want to proceed (JS)');
                return false;

            }

        }

    </script>

with this
 
<apex:commandButton styleClass="btn" action="{!saveRelease}" value="Submit" title="Submit" onclick="return checkField();" />

 
Amanda Byrne- Carolina Tiger RescueAmanda Byrne- Carolina Tiger Rescue
For whatever reason the javascript solution worked fine on my personal ipad, but failed to work on the organization ipads that would be using the app.

My ipad is a couple of years old, and was running 7.something.
The work ipads are Air2 and running iOS 8.4

But I found another, more elegant solution. Part of the reason I was having so many issues with the different apex solutions I'd been trying was because they were not re-querying my extension controller. 

I had a 'well, duh' moment when I was testing a different aspect of the app, and tried a simple if-then solution in the "saveRelease" function, so the code now looks like this (I was also unfamiliar with the 'addError' option):

Page Code:
<title2>I agree to the above: </title2>
                                                                
                <apex:inputField required="true" styleClass="detail" id="agreementField" value="{!Release_Form__c.DTCI_Agreement__c}">                                    
                    <apex:param name="Type" value="{!Release_Form__c.DTCI_Agreement__c}" assignTo="{!agreement}" />             
                </apex:inputField>
                                         
            <br/>                           
            <apex:commandButton styleClass="btn" action="{!saveRelease}" value="Submit" title="Submit" immediate="false" />            
            <br/>
and the extension Controller code looks like this:
//Release Form Save Button
    public PageReference saveRelease(){
        
        agreement = releaseForm.DTCI_Agreement__c;
        parentGuardianName = releaseForm.DTCI_Parent_or_Guardian__c;
        
         if(agreement == FALSE) {
            releaseForm.DTCI_Agreement__c.addError('Please check the agreement box to proceed.');
            return null;
         } else {
        
            Release_Form__c newRel = new Release_Form__c(DTCI_Attendee__c = attendeeId, 
                                                         DTCI_Parent_or_Guardian__c = parentGuardianName, 
                                                         DTCI_Agreement__c = agreement);        
            insert newRel;
                         
    }

It works beautifully.
 
This was selected as the best answer