The <apex:form>
component is a must-have wrapper for interactive elements in Visualforce. It allows user input, button actions, and AJAX interactions to be processed by the server.
Why It’s Important
- Enables form submissions to the controller.
- Required for components like
<apex:commandButton>
,<apex:inputText>
, etc. - Handles page state between requests.
Basic Syntax
xmlCopyEdit<apex:form>
<!-- input and buttons go here -->
</apex:form>
Example: Simple Form with Input and Button
xmlCopyEdit<apex:page controller="SimpleFormController">
<apex:form>
<apex:inputText value="{!name}" label="Enter your name" />
<apex:commandButton value="Submit" action="{!greetUser}" />
<p>{!message}</p>
</apex:form>
</apex:page>
Apex Controller:
apexCopyEditpublic class SimpleFormController {
public String name { get; set; }
public String message { get; set; }
public void greetUser() {
message = 'Hello, ' + name + '!';
}
}
Key Points
- Only one
<apex:form>
per page is best practice, unless needed otherwise. - Works with AJAX components like
<apex:actionSupport>
and<apex:actionFunction>
.
Use Cases
- Login forms
- Input data submission
- Triggering server-side logic