Form Input Fields
Text-based form fields all work the same way in Trongate. Learn one, know them all.
The Pattern
Every input helper follows this structure:
- $name - the field name (required)
- $value - the default value (optional)
- $attributes - array of HTML attributes (optional)
Available Input Types
| Function | HTML Type | Use Case |
|---|---|---|
text |
General text input | |
email |
Email addresses | |
password |
Password fields | |
number |
Numeric input | |
search |
Search boxes | |
hidden |
Hidden fields |
Basic Examples
Text Input
With a Default Value
With Attributes
Email Fields
Use for email addresses. Browsers will validate the format automatically:
Password Fields
Use for password input. The value is never displayed:
Never pass a default value to form_password() in production. Passwords should never pre-populate.
Number Fields
Use for numeric input with optional min, max, and step:
Search Fields
Use for search boxes. Some browsers add a clear button automatically:
Hidden Fields
Use for data that needs to be submitted but not displayed:
How to Repopulate Forms After Validation Errors
When a form fails validation, you need to redisplay it with the user's entered values. In Trongate, this follows the MVC pattern:
Controller Example
View Example
The controller fetches the posted data using , then passes it to the view. The view receives these values as variables.
Real-World Example: User Registration Form
First, the controller prepares the data:
Then the view displays the form:
Common Attributes
Here are the most commonly used attributes:
| Attribute | Purpose | Example |
|---|---|---|
placeholder |
Hint text | ['placeholder' => 'Enter name'] |
id |
Element ID | ['id' => 'username-field'] |
required |
HTML5 validation | ['required' => 'required'] |
maxlength |
Character limit | ['maxlength' => '50'] |
autocomplete |
Browser autocomplete | ['autocomplete' => 'email'] |
readonly |
Prevent editing | ['readonly' => 'readonly'] |
disabled |
Disable field | ['disabled' => 'disabled'] |
Multiple Attributes Example
Controller:
View:
Working with Arrays
Field names can use array syntax for grouping related data. The controller handles the data extraction:
View:
All input helpers automatically escape attribute values for XSS protection. You never have to worry about it.
We're continually improving the Trongate documentation. If anything is incorrect, unclear, incomplete, or could be better, we'd genuinely appreciate your input.
Share your thoughts in the Documentation Feedback.