Skip to main content

Multiple Forms in Visualforce Page

Flags Visualforce pages with multiple <apex:form> components, which can cause inconsistent behavior, complex logic, and maintenance difficulties.

David Martin avatar
Written by David Martin
Updated yesterday

Overview

This rule flags Visualforce Pages that contain multiple <apex:form> components. While technically allowed, having more than one form on a single page can lead to unexpected behaviors, especially around component state, data binding, and command button actions.

Code reviews classifies this as a Error because it doesn’t always break functionality, but it can introduce subtle bugs and make maintenance and testing harder.

Why This Matters

Having multiple forms on a single page can lead to inconsistent component states, unpredictable partial page updates, complex controller logic, and confusion over submission behavior. It may also increase the likelihood of view state errors or performance issues. Using a single, well-structured form simplifies control, improves stability, and makes the page easier to maintain.

What Triggers This Rule

Code reviews flags Visualforce Pages that:

  • Include more than one <apex:form> component

  • Don’t use clear separation of responsibilities between forms

  • Nest forms or scatter them across unrelated sections of a page

Example of a Violation

<apex:page controller="MyContactController">

<apex:form>
<apex:pageBlock title="Contact Details">
<apex:pageBlockSection columns="1">
<apex:inputText label="First Name" value="{!contact.FirstName}" />
<apex:inputText label="Last Name" value="{!contact.LastName}" />
<apex:commandButton action="{!saveContactDetails}" value="Save Contact" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

<br/><br/>

<apex:form>
<apex:pageBlock title="Email Preferences">
<apex:pageBlockSection columns="1">
<apex:inputCheckbox label="Receive Newsletter" value="{!contact.Receive_Newsletter__c}" />
<apex:commandButton action="{!updateEmailPreferences}" value="Update Preferences" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

</apex:page>

Recommended Approach

  • Consolidate inputs into a single form where possible

  • Use <apex:actionRegion> or <apex:actionFunction> if partial updates are needed

  • Utilizing apex:actionStatus can help by displaying the status of an AJAX update request, preventing duplicate submissions

  • If multiple forms are absolutely necessary, isolate their logic clearly in the controller

  • Clearly label buttons and sections to indicate intent

Summary

More forms often mean more problems. Use one form per page whenever possible to ensure a cleaner, more predictable user experience.

Code review flags multiple forms as a error to encourage simpler, safer page designs.

Did this answer your question?