Skip to main content

Code reviews rule: Multiple forms in Visualforce page

Written by David Martin
Updated this week

Multiple forms in Visualforce page

Why is this an issue?

Using multiple <apex:form> tags on a single Visualforce page results in each form rendering its own copy of the page's view state. This increases the overall page payload and can negatively impact performance. Larger or more complex pages are therefore more likely to approach the 170KB view state limit.

Additionally, multiple forms can cause:

  • Unexpected behavior when submitting data

  • Increased page load times

  • More complex JavaScript interactions

Examples

Example of incorrect code:

<apex:page controller="MyController">
<apex:form>
<apex:inputField value="{!account.Name}"/>
<apex:commandButton value="Save Name" action="{!saveName}"/>
</apex:form>

<apex:form>
<apex:inputField value="{!account.Phone}"/>
<apex:commandButton value="Save Phone" action="{!savePhone}"/>
</apex:form>
</apex:page>

Example of correct code:

<apex:page controller="MyController">
<apex:form>
<apex:inputField value="{!account.Name}"/>
<apex:inputField value="{!account.Phone}"/>
<apex:commandButton value="Save" action="{!save}"/>
</apex:form>
</apex:page>

How can I fix violations?

Consolidate multiple forms into a single <apex:form>:

  1. Move all input components into one form

  2. Use <apex:actionRegion> to limit which components are processed during specific actions

  3. Use reRender attributes to update specific sections without full page refreshes

When should I disable this rule?

You may dismiss specific violations when separate forms are technically required, such as when embedding third-party content that requires its own form element.

Resources

Did this answer your question?