Introduction
Basic Concepts
Understanding Routing
Intercepting Requests
Module Fundamentals
Database Operations
Templates
Helpers
Form Handling
Form Validation
Working With Files
Image Manipulation
Working With Dates & Times
Language Control
Authorization & Authentication
Form Validation Basics
In Trongate v2, validation is handled by the validation module located at modules/validation/. Like all modules, it loads automatically when you reference it:
The Pattern
- Set validation rules
- Run validation
- Handle result
Two Syntax Options
Option 1: Pipe Syntax (Simple)
$this->validation->set_rules('username', 'username', 'required|min_length[3]');
$this->validation->set_rules('email', 'email address', 'required|valid_email');
$result = $this->validation->run();
Option 2: Array Syntax (Organized)
$rules = [
'username' => ['label' => 'username', 'required' => true, 'min_length' => 3],
'email' => ['label' => 'email address', 'required' => true, 'valid_email' => true]
];
$result = $this->validation->run($rules);
Built-In Rules
required | Field cannot be empty |
min_length[n] | Minimum character length |
max_length[n] | Maximum character length |
valid_email | Valid email format |
numeric | Must be a number |
integer | Must be an integer |
matches[field] | Must match another field |
Checkbox Validation: Special Consideration
Checkboxes behave differently because unchecked checkboxes don't submit data. An unchecked checkbox returns `''` (empty string) from post().
Important: If a checkbox is optional (not required), don't add required rule to it. The validation will correctly handle the empty string.
// For REQUIRED checkbox (must be checked):
$this->validation->set_rules('terms', 'terms and conditions', 'required');
// User must check the box
// For OPTIONAL checkbox:
// No validation rule needed
// Unchecked = '', Checked = '1'
$data['newsletter'] = (int) (bool) post('newsletter', true); // 0 or 1
Complete Example with Checkbox
public function submit(): void {
// Set rules
$this->validation->set_rules('email', 'email address', 'required|valid_email');
$this->validation->set_rules('password', 'password', 'required|min_length[8]');
$this->validation->set_rules('terms', 'terms and conditions', 'required'); // Required checkbox
// Run validation
if ($this->validation->run() === true) {
// Success: save data
$data['email'] = post('email', true);
$data['password'] = post('password', true);
// Convert checkbox for database
$data['terms_accepted'] = (int) (bool) post('terms', true); // 1 if checked
$data['newsletter'] = (int) (bool) post('newsletter', true); // Optional: 0 or 1
$this->db->insert($data, 'users');
redirect('users/manage');
} else {
// Failure: redisplay form
$this->create();
}
}
Key Points
- Use lowercase field labels in
set_rules()for natural error messages - Use
post('field', true)for consistency with cleaning - CSRF protection is automatic
- Errors store in session, display with
validation_errors() - Optional fields skip validation when empty
- Checkboxes: Use
requiredonly if user must check the box - For optional checkboxes, no validation rule is needed
Validation fails → form redisplays with errors.
Validation passes → data saves, user redirects.