The <apex:inputText>
component is used to create single-line text input fields on Visualforce pages. It allows users to enter simple text, such as names, addresses, or any other non-sensitive data.
Why Use <apex:inputText>
?
- Collect plain text input from users.
- Ideal for user entries like names, email addresses, and general comments.
- Supports basic text-based user inputs, offering flexibility for different use cases.
Basic Syntax
xmlCopyEdit<apex:inputText value="{!textVariable}" label="Enter Text"/>
value
: Binds the input field to an Apex variable that holds the entered text.label
: Provides a label for the input field, indicating the type of data expected.
Example: Collecting a Name
xmlCopyEdit<apex:page controller="TextInputController">
<apex:form>
<apex:inputText value="{!userName}" label="Enter your name"/>
<apex:commandButton value="Submit" action="{!submitForm}"/>
</apex:form>
</apex:page>
Apex Controller:
apexCopyEditpublic class TextInputController {
public String userName { get; set; }
public void submitForm() {
System.debug('User Name: ' + userName);
}
}
Key Attributes
value
: Binds the text entered by the user to an Apex variable.label
: A text label displayed next to the input field.required
: Marks the input field as mandatory.maxlength
: Limits the number of characters a user can enter.size
: Defines the visible width of the input field in characters.
Use Cases
- Form Fields: Collect user information like names, addresses, or other general details.
- Search Forms: Collect search terms or keywords.
- Feedback Forms: Gather text-based feedback or comments.