The <apex:input> component is used to create input fields on your Visualforce page, allowing users to enter data. It supports various types of inputs like text, email, number, and password fields.
Why Use <apex:input>?
- Collect data from users for forms or surveys.
- Support multiple input types, including text, email, number, password, etc.
- Bind input values to Apex controllers for processing.
Basic Syntax
xmlCopyEdit<apex:input value="{!yourApexVariable}" />
value: Binds the input field to a controller variable.
Example: Basic Input Field
xmlCopyEdit<apex:page controller="InputExampleController">
<apex:form>
<apex:input value="{!userName}" label="Enter your name"/>
<apex:commandButton value="Submit" action="{!submitForm}" />
</apex:form>
</apex:page>
Apex Controller:
apexCopyEditpublic class InputExampleController {
public String userName { get; set; }
public void submitForm() {
System.debug('User Name: ' + userName);
}
}
Key Attributes
value: Binds the input field to a variable in your Apex controller.label: Provides a label for the input field.required: Makes the input field mandatory.maxlength: Limits the number of characters that can be entered.
Use Cases
- Creating user registration or login forms.
- Collecting feedback from users.
- Gathering search or filter parameters.