Trongate PHP Framework Docs
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
Tips And Best Practices

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

  1. Set validation rules
  2. Run validation
  3. Handle result

Two Syntax Options

Option 1: Pipe Syntax (Simple)

Option 2: Array Syntax (Organized)

Built-In Rules

requiredField cannot be empty
min_length[n]Minimum character length
max_length[n]Maximum character length
valid_emailValid email format
numericMust be a number
integerMust 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 .

Important: If a checkbox is optional (not required), don't add required rule to it. The validation will correctly handle the empty string.

Complete Example with Checkbox

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 required only 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.