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
Dman100Dman100 

validating multiple pageBlocks independently

I have a VF page that includes two pageBlock sections that call update methods.  Is it possible to validate only the pageBlock section where the update method is getting executed?

 

Here is my VF code:

 

<apex:page Controller="AccountMaintenanceController"> <h1>Manage Salesperson Territory Assignments</h1> <apex:form > <apex:pageBlock title="Update Specific District or Account Ownership" mode="edit"> <apex:pageBlockButtons > <apex:commandButton action="{!updateChildAccountOwnership}" value="Update" rerender="UpdateChildAccountsSuccessMsg" /> <apex:commandButton action="{!cancel}" value="Cancel" immediate="true" /> </apex:pageBlockButtons> <apex:pageMessages rendered="true" id="UpdateChildAccountsSuccessMsg" /> <apex:pageBlockSection title="Used to update owner of specified account and related child accounts" columns="1"> <apex:pageBlockSectionItem > <apex:outputLabel value="Change Owner To" for="new_account_owner" /> <apex:inputField value="{!AccountToByParent.ownerId}" id="new_account_owner" /> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="District Account to update" for="account_name" /> <apex:inputField value="{!DistrictAccount.parentId}" id="account_name" required="true" /> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> <apex:pageBlock title="Update Account Ownership by State" mode="edit"> <apex:pageBlockButtons > <apex:commandButton action="{!updateAccountByState}" value="Update" rerender="UpdateOwnerByStateSuccessMsg" /> <apex:commandButton action="{!cancel}" value="Cancel" immediate="true" /> </apex:pageBlockButtons> <apex:pageMessages rendered="true" id="UpdateOwnerByStateSuccessMsg" /> <apex:pageBlockSection title="Used to update owner of accounts by selected state" columns="1"> <apex:pageBlockSectionItem > <apex:outputLabel value="Change Owner To" for="new_account_owner" /> <apex:inputField value="{!AccountToByState.ownerId}" id="new_account_owner" /> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="State" for="state" /> <apex:inputField value="{!State.State__c}" id="state" required="true" /> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>

 

controller class:

 

public class AccountMaintenanceController { private Account acctToByParent = new Account(); private Account parentAcct = new Account(); private Account acctToByState = new Account(); private Account state = new Account(); public PageReference updateChildAccountOwnership() { Account parent = [Select Id, OwnerId From Account where Id =: parentAcct.ParentId]; List<Account> childAccts = [Select Id, OwnerId From Account where ParentId =: parent.Id]; parent.ownerId = acctToByParent.ownerId; DbUtil.save(parent); List<Account> lst = new List<Account>(); for (Account acct: childAccts) { acct.OwnerId = acctToByParent.OwnerId; lst.add(acct); } DbUtil.save(lst); ApexPages.Message msg = new ApexPages.message(ApexPages.Severity.INFO, 'Account ownership updated successfully.'); ApexPages.addMessage(msg); return null; } public PageReference updateAccountByState() { List<Account> accts = [Select Id, OwnerId From Account where state__c =: state.state__c]; List<Account> lst = new List<Account>(); for (Account acct: accts) { acct.OwnerId = acctToByState.OwnerId; lst.add(acct); } DbUtil.save(lst); ApexPages.Message msg = new ApexPages.message(ApexPages.Severity.INFO, 'Account ownership updated successfully.'); ApexPages.addMessage(msg); return null; } public PageReference cancel() { PageReference newpage = new PageReference(System.currentPageReference().getURL()); newpage.setRedirect(true); return newpage; } public Account getAccountToByParent() { return acctToByParent; } public Account getDistrictAccount() { return parentAcct; } public Account getAccountToByState() { return acctToByState; } public Account getState() { return state; }

 

 

So, when I click the update button in either one of the pageBlock sections, it validates both pageBlock sections, so I cannot execute the update methods in the pageBlock sections independently.

 

Thanks for any help.

Best Answer chosen by Admin (Salesforce Developers) 
sornasorna
Refer to <apex:actionRegion> tag in component reference. I think it will help you in this case.

All Answers

sornasorna
Refer to <apex:actionRegion> tag in component reference. I think it will help you in this case.
This was selected as the best answer
WesNolte__cWesNolte__c

Hey

 

Wrap each pageblocksection in it's own form and only that form will be submitted when you click the buttons within that specific form. 

 

Cheers,

Wes

jwetzlerjwetzler

Actually Wes, apex:actionRegion is what you want to use.

 

It's a best practice to limit the number of forms on your page to one,  as multiple forms cause multiple instances of viewstate on your page, which makes you much more likely to hit the viewstate limit.

 

apex:actionRegion is specifically for this purpose.

WesNolte__cWesNolte__c

Ahhh, always nice to get the official facts:) Knowledge++

 

Wes