[SalesForce] Validation of required fields or disable Save button

With my existing VF page and controller, what do I need to do to be able keep the 'save' method from being called by an accidental click? Can I add validation or disable the save button?

When the Saved button is clicked and all of the required fields are not Null. A message appears notifying the user that the record was saved. VF is closed after 5 seconds. Account page reloads.

VF page

<apex:page standardController="Merchandising__c"  extensions="MerchandisingController"  standardstylesheets="true" showheader="false"  >
 <script>
   function Closewindow(){
   window.close();
   window.opener.location.href="/{!$CurrentPage.parameters.someId}";
   window.top.refresh();
}
</script>

<div style="background-color:green;height:500px;width:800px;">
   <apex:form id="Merchandising">
    <apex:sectionHeader title="Choose Merchandizing"/>
    <apex:pageBlock title="Demo Page">
    Delivery Date = '05/18/2018';
        <apex:pageBlockSection columns="2">             
          <apex:inputField value="{!merch.Merchandise__c}"/>
          <apex:inputField value="{!merch.Date_Displayed__c}"/>
          <apex:inputField value="{!merch.Merchandise_Category__c}"/>
          <apex:outputfield value="{!merch.Date_Delivered__c}"  html-disabled="true" />
          <apex:inputField value="{!merch.Merchandise_SubCategory__c}"/>                
          <apex:inputField value="{!merch.Date_Removed_Replaced__c}"/>
        </apex:pageBlockSection>
        <Br></Br><Br></Br><Br></Br>    

        <div align="center" draggable="false" >
        <apex:commandButton action="{!save}" value="Save" onclick="setTimeout(Closewindow, 5000)" id="saveButton" reRender="Panel" />        
        &nbsp;&nbsp;&nbsp;&nbsp;
        <apex:commandButton value="Cancel" onclick="window.close();" immediate="true"/>
        </div>

        <apex:outputPanel id="Panel" style="font-size:16px;color:red">
            <apex:messages />
        </apex:outputPanel>
    </apex:pageBlock>
</apex:form>
       &nbsp;&nbsp;&nbsp;&nbsp;

</div>     
</apex:page>

Controller

public with sharing class MerchandisingController {

public Merchandising__c merch{get;set;}
public string message {get;set;} 

public MerchandisingController (ApexPages.StandardController controller) 
{
    merch = new Merchandising__c();
    merch.Account__c= ApexPages.CurrentPage().getparameters().get('someId');
}

 public void save() 
 {
    insert merch; 
    message = 'Record Created Successfully.Please add pictures of the disply to the Account. Thank you!'; 
    ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,message )); 
 }  
}

Best Answer

The code above restricts the user from perform an accidental click, while the save is being performed, once the Save button is clicked.

Validations are required to ensure consistency in the input data. In order to restrict a Save, when fields are left empty, you can use the required attribute on <apex:inputField> as below.

<apex:inputField value="{!merch.Merchandise__c}" required="true"/>

However, is it important to understand that this technique will only mandate the field, to have a value only on this page. In case, you have the same field being populated with values through other mechanisms such as via an API integration or through a data upload, and require the same mandate to be applied there, you should mark the field as required on the respective Sobject rather than on the Visualforce page.

Related Topic