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
djathomsondjathomson 

Disable form submit

Hello,

 

I am trying to create a visualforce "wizard". I would like to disable the form submit on pressing enter (particularly when all the fields are not filled out), and force the user to click "Next" instead. Any advice would be appreciated.

 

David

Best Answer chosen by Admin (Salesforce Developers) 
MATTYBMEMATTYBME

You do not have to reference the Save button. In case you may not understand the functionality of a wizard when you go to a next step in your wixard design the controller must save the first steps information. It gets saved in a temp data store and will not hard save until the last step in your wizard.

 

Also, if you want the fields on each step to be filled you need to set them as required and build into your page pageMessages so as to display the required field error message upon selecting next in your step.

 

So here is an example first step of a wizard:

 

<apex:page standardController="Job_Application__c" extensions="JobAppWizard" tabStyle="Job_Application__c"> <apex:includeScript value="{!$Resource.utilityJS}" /> <apex:sectionHeader title="Job Application" subtitle="Step 1 of 3"/> <apex:form > <apex:pageBlock id="theBlock" title="Job Application Edit" mode="edit"> <apex:pageBlockButtons > <apex:commandButton action="{!step2}" value="Next"/> <apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true" style="margin-left: 2em"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Information" columns="1"> <apex:inputField value="{!JobApplication.Status__c}"/> <apex:inputField value="{!JobApplication.Stage__c}"/> <apex:inputField value="{!JobApplication.Position__c}" required="true"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>

 

 and the extension:

 

public class JobAppWizard {

// These properties will be used to maintain state

// Search results are not transient in case the user accidentally selects
// the wrong one and wants to go back
public List<Candidate__c> results {
get{
if(results == null) results = new List<Candidate__c>();
return results;
}
set;
}

public ApexPages.standardController controller {get; set;}

public String searchText {
get;
set{
value = value.endsWith('*') ? value : value + '*';
searchText = value;
}
}

public Boolean showSearch {
get{
if (showSearch == null) showSearch = true;
return showSearch;
}
set;
}

public Job_Application__c jobApplication { get; set; }

public Candidate__c candidate {
get{
if (candidate == null) candidate = new Candidate__c();
return candidate;
}
set;
}

public ID candidateId { get; set; }

public JobAppWizard(ApexPages.StandardController stdController) {
// constructor
controller = stdController;
this.jobApplication = (Job_Application__c)stdController.getRecord();
if ((ApexPages.currentPage().getParameters().get('posId') != null)&&
(ApexPages.currentPage().getParameters().get('posId') != '')){
jobApplication.Position__c = ApexPages.currentPage().getParameters().get('posId');
}
}

// The next 3 methods control navigation through
// the wizard. Each returns a PageReference for one of the 3 pages
// in the wizard. Note that the redirect attribute does not need to
// be set on the PageReference because the URL does not need to change
// when users move from page to page.
public PageReference step1() {
return Page.jobAppStep1;
}
public PageReference step2() {
return Page.jobAppStep2;
}
public PageReference step3() {
if (candidate.last_name__c == null){
candidate.last_name__c.addError('Last Name is required');
return null;
} else {
return Page.jobAppStep3;
}
}

// this function is called by step2 & step3 pages in the action attribute,
// in case anyone tries to go directly to them it will navigate the user back to step1
public PageReference checkPosition(){
if (jobApplication.Position__c == null) {
PageReference newRef = Page.jobAppStep1;
newRef.setRedirect(true);
return newRef;
} else {
return null;
}
}

public PageReference doSearch() {
results = (List<Candidate__c>)[FIND :searchText IN ALL FIELDS RETURNING Candidate__c(Id, First_Name__c, Last_Name__c, Email__c, City__c, State_Province__c, Phone__c)] [0];
return null;
}

// doing a separate query here to keep the view state down
public PageReference SelectCandidate() {
candidate = [select id,first_name__c,last_name__c,phone__c,email__c,mobile__c, street_address_1__c,street_address_2__c,city__c,state_province__c,zip_postal_code__c,country__c from Candidate__c where id=:candidateId];
return Page.jobAppStep3;
}

public PageReference NewCandidate(){
if (!showSearch) {
candidate = new Candidate__c();
candidateId = null; // reset the id so we know what to do upon save
}
return null;
}

public PageReference save() {
// catches need to return null; redo using ApexPages.addMessages(e) & <apex:pageMessages>
if (candidateId == null) {
// it's a brand spanking new candidate so we need to insert it first
try{
insert candidate;
System.debug('new candidate=' + candidate);
} catch (System.DmlException e) {
ApexPages.addMessages(e);
return null;
}
}
System.debug('jobApplication=' + jobApplication);
jobApplication.Candidate__c = candidate.id;
try{
insert jobApplication;
} catch (System.DmlException e) {
ApexPages.addMessages(e);
return null;
}
controller = new ApexPages.standardController(jobApplication);
return controller.view();
}
}

 The section in blue is what you need to look at closely.