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:
form_[type]($name, $value, $attributes);- $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 | |
url |
URL / website addresses | |
hidden |
Hidden fields |
Basic Examples
Text Input
echo form_input('username');
// Output: <input type="text" name="username">With a Default Value
echo form_input('username', 'John Doe');
// Output: <input type="text" name="username" value="John Doe">With Attributes
$attributes = [
'placeholder' => 'Enter your username',
'maxlength' => '50'
];
echo form_input('username', '', $attributes);
// Output: <input type="text" name="username" value="" placeholder="Enter your username" maxlength="50">Email Fields
Use for email addresses. Browsers will validate the format automatically:
echo form_email('user_email', '', ['placeholder' => '[email protected]']);Password Fields
Use for password input. The value is never displayed:
echo form_password('password', '', ['placeholder' => 'Enter password']);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:
$attributes = [
'min' => '1',
'max' => '100',
'step' => '1'
];
echo form_number('quantity', '1', $attributes);Search Fields
Use for search boxes. Some browsers add a clear button automatically:
echo form_search('q', '', ['placeholder' => 'Search...']);URL Fields
Use for website addresses. Browsers validate the URL format automatically:
echo form_url('website', '', ['placeholder' => 'https://example.com']);Hidden Fields
Use for data that needs to be submitted but not displayed:
echo form_hidden('user_id', '42');
// Output: <input type="hidden" name="user_id" value="42">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
public function create(): void {
// Fetch posted values (returns empty strings if form hasn't been submitted)
$data['product_code'] = post('product_code', true);
$data['email'] = post('email', true);
// Pass data to view
$this->view('product_form', $data);
}View Example
<?php
echo form_open('products/submit');
echo form_label('Product Code');
echo form_input('product_code', $product_code);
echo form_label('Email');
echo form_email('email', $email);
echo form_submit('submit', 'Save');
echo form_close();
?>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:
public function register(): void {
$data['full_name'] = post('full_name', true);
$data['email'] = post('email', true);
$this->view('register_form', $data);
}Then the view displays the form:
echo form_open('users/submit_registration');
echo form_label('Full Name');
echo form_input('full_name', $full_name, ['placeholder' => 'John Smith', 'required' => 'required']);
echo form_label('Email Address');
echo form_email('email', $email, ['placeholder' => '[email protected]', 'required' => 'required']);
echo form_label('Password');
echo form_password('password', '', ['placeholder' => 'Minimum 8 characters', 'minlength' => '8']);
echo form_label('Confirm Password');
echo form_password('password_confirm', '', ['placeholder' => 'Re-enter password']);
echo form_submit('submit', 'Create Account');
echo form_close();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:
public function create_product(): void {
$data['product_code'] = post('product_code', true);
$this->view('product_form', $data);
}View:
$attributes = [
'id' => 'product-code',
'placeholder' => 'e.g., PROD-001',
'maxlength' => '20',
'required' => 'required',
'pattern' => '[A-Z0-9-]+'
];
echo form_input('product_code', $product_code, $attributes);Working with Arrays
Field names can use array syntax for grouping related data. The controller handles the data extraction:
public function create_user(): void {
// Use dot notation to access nested data
$data['first_name'] = post('user.first_name', true);
$data['last_name'] = post('user.last_name', true);
$data['email'] = post('user.email', true);
$this->view('user_form', $data);
}View:
echo form_input('user[first_name]', $first_name);
echo form_input('user[last_name]', $last_name);
echo form_email('user[email]', $email);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.