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

Validation Rules Reference

This page provides a complete reference of all built-in validation rules available in Trongate v2. Each rule validates a specific aspect of form data, and most rules are optional - fields only validate when they contain data (except required).

Optional Fields: All validation rules except required skip validation if the field is empty. To validate an empty field, use the required rule first.

Quick Reference Table

Rule Parameter Purpose
required - Field cannot be empty
numeric - Must be a number
integer - Must be a whole number
decimal - Must be a decimal number
valid_email - Must be valid email address
valid_url - Must be valid URL
valid_ip - Must be valid IP address
valid_date - Must be valid date (YYYY-MM-DD)
valid_time - Must be valid time (HH:MM or HH:MM:SS)
valid_datetime_local - Must be valid datetime (YYYY-MM-DDTHH:MM)
valid_month - Must be valid month (YYYY-MM)
valid_week - Must be valid week (YYYY-W##)
min_lengthnumberMinimum character length
max_lengthnumberMaximum character length
exact_lengthnumberExact character length
greater_thannumberMust be greater than value
less_thannumberMust be less than value
matchesfieldMust match another field
allowed_typesextensionsAllowed file types
max_sizekilobytesMaximum file size
min_sizekilobytesMinimum file size
is_image - Must be valid image
max_widthpixelsMaximum image width
min_widthpixelsMinimum image width
max_heightpixelsMaximum image height
min_heightpixelsMinimum image height
exact_widthpixelsExact image width
exact_heightpixelsExact image height
square - Image must be square

Standard Rules

required

The field cannot be empty. This is the only rule that validates empty fields - all other rules skip validation if the field is empty.

Syntax:

PHP
$this->validation->set_rules('email', 'email address', 'required');

Error Message:

The [label] field is required.

numeric

The field must contain a numeric value (integer or decimal, positive or negative).

Syntax:

PHP
$this->validation->set_rules('age', 'age', 'numeric');

Valid Values: 42, -15, 3.14, -2.5

Invalid Values: abc, 12x, 1,000

Error Message:

The [label] field must be numeric.

integer

The field must contain a whole number (no decimal places, positive or negative).

Syntax:

PHP
$this->validation->set_rules('quantity', 'quantity', 'integer');

Valid Values: 42, -15, 0

Invalid Values: 3.14, abc, 42.0

Error Message:

The [label] field must be an integer.

decimal

The field must contain a decimal number (with or without decimal places).

Syntax:

PHP
$this->validation->set_rules('price', 'price', 'decimal');

Valid Values: 19.99, 10, -3.5

Invalid Values: $19.99, abc

Error Message:

The [label] field must be a decimal number.

Format Rules

valid_email

The field must contain a valid email address.

Syntax:

PHP
$this->validation->set_rules('email', 'email address', 'valid_email');

Valid Values: [email protected], [email protected]

Invalid Values: user@, @example.com, user [email protected]

Error Message:

The [label] field must contain a valid email address.

valid_url

The field must contain a valid URL (with protocol).

Syntax:

PHP
$this->validation->set_rules('website', 'website', 'valid_url');

Valid Values: https://example.com, http://example.com/path

Invalid Values: example.com, not a url

Error Message:

The [label] field must contain a valid URL.

valid_ip

The field must contain a valid IP address (IPv4 or IPv6).

Syntax:

PHP
$this->validation->set_rules('ip_address', 'IP address', 'valid_ip');

Valid Values: 192.168.1.1, 2001:0db8:85a3::8a2e:0370:7334

Invalid Values: 256.256.256.256, not an ip

Error Message:

The [label] field must contain a valid IP address.

Date and Time Rules

valid_date

The field must be a valid date in YYYY-MM-DD format.

Syntax:

PHP
$this->validation->set_rules('birth_date', 'birth date', 'valid_date');

Valid Values: 2024-03-18, 1999-12-25

Invalid Values: 03/18/2024, 2024-13-01, 2024-02-30

Error Message:

The [label] field must be a valid date in YYYY-MM-DD format.

valid_time

The field must be a valid time in HH:MM or HH:MM:SS format (24-hour).

Syntax:

PHP
$this->validation->set_rules('meeting_time', 'meeting time', 'valid_time');

Valid Values: 14:30, 09:15:45, 23:59:59

Invalid Values: 2:30 PM, 25:00, 14:60:00

Error Message:

The [label] field must be a valid time in HH:MM or HH:MM:SS format.

valid_datetime_local

The field must be a valid datetime in YYYY-MM-DDTHH:MM format (ISO 8601 local datetime).

Syntax:

PHP
$this->validation->set_rules('appointment', 'appointment', 'valid_datetime_local');

Valid Values: 2024-03-18T14:30, 2024-12-25T23:59

Invalid Values: 2024-03-18 14:30, 03/18/2024T14:30

Error Message:

The [label] field must be a valid datetime in YYYY-MM-DDTHH:MM format.

valid_month

The field must be a valid month in YYYY-MM format.

Syntax:

PHP
$this->validation->set_rules('billing_month', 'billing month', 'valid_month');

Valid Values: 2024-03, 2024-12

Invalid Values: 2024-13, 03-2024, March 2024

Error Message:

The [label] field must be a valid month in YYYY-MM format.

valid_week

The field must be a valid week in YYYY-W## format (ISO 8601 week date).

Syntax:

PHP
$this->validation->set_rules('work_week', 'work week', 'valid_week');

Valid Values: 2024-W12, 2024-W52

Invalid Values: 2024-W53 (if year doesn't have 53 weeks), W12-2024

Error Message:

The [label] field must be a valid week in YYYY-W## format.

Length Rules

min_length

The field must be at least the specified number of characters long.

Syntax:

PHP
$this->validation->set_rules('password', 'password', 'min_length[8]');
$this->validation->set_rules('username', 'username', 'min_length[3]');

Valid for min_length[3]: abc, john, user123

Invalid for min_length[3]: ab, x

Error Message:

The [label] field must be at least [param] characters long.

max_length

The field cannot exceed the specified number of characters.

Syntax:

PHP
$this->validation->set_rules('bio', 'biography', 'max_length[500]');
$this->validation->set_rules('city', 'city', 'max_length[50]');

Valid for max_length[50]: New York, Los Angeles

Invalid for max_length[50]: Any string longer than 50 characters

Error Message:

The [label] field cannot exceed [param] characters.

exact_length

The field must be exactly the specified number of characters long.

Syntax:

PHP
$this->validation->set_rules('postal_code', 'postal code', 'exact_length[5]');
$this->validation->set_rules('zip', 'ZIP code', 'exact_length[5]');

Valid for exact_length[5]: 12345, abcde

Invalid for exact_length[5]: 1234, 123456

Error Message:

The [label] field must be exactly [param] characters long.

Comparison Rules

greater_than

The field must contain a value greater than the specified number.

Syntax:

PHP
$this->validation->set_rules('age', 'age', 'greater_than[18]');
$this->validation->set_rules('quantity', 'quantity', 'greater_than[0]');

Valid for greater_than[18]: 19, 25, 100

Invalid for greater_than[18]: 18, 17, 0

Error Message:

The [label] field must be greater than [param].

less_than

The field must contain a value less than the specified number.

Syntax:

PHP
$this->validation->set_rules('discount', 'discount', 'less_than[100]');
$this->validation->set_rules('rating', 'rating', 'less_than[5]');

Valid for less_than[100]: 99, 50, 0

Invalid for less_than[100]: 100, 101

Error Message:

The [label] field must be less than [param].

matches

The field must match the value of another field. Commonly used for password confirmation.

Syntax:

PHP
$this->validation->set_rules('password', 'password', 'required|min_length[8]');
$this->validation->set_rules('confirm_password', 'password confirmation', 'required|matches[password]');

Valid: Both fields contain the same value

Invalid: Fields contain different values

Error Message:

The [label] field must match the [param] field.

Note: The parameter is the name of the field to compare against, not a label.

File Validation Rules

allowed_types

The uploaded file must be one of the specified file types (by extension).

Syntax:

PHP
$this->validation->set_rules('document', 'document', 'allowed_types[pdf,doc,docx]');
$this->validation->set_rules('resume', 'resume', 'allowed_types[pdf,txt]');

Parameters: Comma-separated list of allowed file extensions (without dots)

Valid for allowed_types[pdf,doc,docx]: resume.pdf, letter.doc, report.docx

Invalid for allowed_types[pdf,doc,docx]: image.jpg, video.mp4

Error Message:

The [label] must be one of the following file types: [param].

max_size

The uploaded file size cannot exceed the specified limit in kilobytes.

Syntax:

PHP
$this->validation->set_rules('photo', 'photo', 'max_size[5000]');  // 5MB
$this->validation->set_rules('document', 'document', 'max_size[10000]');  // 10MB

Parameters: Maximum file size in kilobytes

Error Message:

The [label] file size must not exceed [param] KB.

min_size

The uploaded file size must be at least the specified limit in kilobytes.

Syntax:

PHP
$this->validation->set_rules('video', 'video', 'min_size[100]');  // At least 100KB

Parameters: Minimum file size in kilobytes

Error Message:

The [label] file size must be at least [param] KB.

Image Validation Rules

is_image

The uploaded file must be a valid image file (JPG, PNG, GIF, or WEBP).

Syntax:

PHP
$this->validation->set_rules('avatar', 'avatar', 'is_image');

Valid File Types: JPG, PNG, GIF, WEBP

Error Message:

The [label] must be a valid image file.

max_width

The image width cannot exceed the specified number of pixels.

Syntax:

PHP
$this->validation->set_rules('banner', 'banner', 'is_image|max_width[1920]');

Parameters: Maximum width in pixels

Error Message:

The [label] image width cannot exceed [param] pixels.

min_width

The image width must be at least the specified number of pixels.

Syntax:

PHP
$this->validation->set_rules('thumbnail', 'thumbnail', 'is_image|min_width[200]');

Parameters: Minimum width in pixels

Error Message:

The [label] image width must be at least [param] pixels.

max_height

The image height cannot exceed the specified number of pixels.

Syntax:

PHP
$this->validation->set_rules('banner', 'banner', 'is_image|max_height[600]');

Parameters: Maximum height in pixels

Error Message:

The [label] image height cannot exceed [param] pixels.

min_height

The image height must be at least the specified number of pixels.

Syntax:

PHP
$this->validation->set_rules('thumbnail', 'thumbnail', 'is_image|min_height[150]');

Parameters: Minimum height in pixels

Error Message:

The [label] image height must be at least [param] pixels.

exact_width

The image width must be exactly the specified number of pixels.

Syntax:

PHP
$this->validation->set_rules('logo', 'logo', 'is_image|exact_width[200]');

Parameters: Exact width in pixels

Error Message:

The [label] image width must be exactly [param] pixels.

exact_height

The image height must be exactly the specified number of pixels.

Syntax:

PHP
$this->validation->set_rules('logo', 'logo', 'is_image|exact_height[200]');

Parameters: Exact height in pixels

Error Message:

The [label] image height must be exactly [param] pixels.

square

The image must be square (equal width and height).

Syntax:

PHP
$this->validation->set_rules('profile_pic', 'profile picture', 'is_image|square');

Valid: Image with width = height (e.g., 200x200, 500x500)

Invalid: Any rectangular image (e.g., 400x300, 1920x1080)

Error Message:

The [label] image must be square (equal width and height).

Combining Rules

Multiple rules can be combined using the pipe character (|):

PHP
// Email must be required, valid email format
$this->validation->set_rules('email', 'email address', 'required|valid_email');

// Username must be required, 3-20 characters
$this->validation->set_rules('username', 'username', 'required|min_length[3]|max_length[20]');

// Image must be valid image, square, max 2MB
$this->validation->set_rules('avatar', 'avatar', 'is_image|square|max_size[2000]');

// Password must match confirmation
$this->validation->set_rules('password', 'password', 'required|min_length[8]');
$this->validation->set_rules('confirm_password', 'password confirmation', 'required|matches[password]');

Early Exit: Validation stops at the first error for each field. If required fails, other rules for that field are not checked.

Complete Example

PHP
public function submit(): void {
    // Set validation rules
    $this->validation->set_rules('first_name', 'first name', 'required|min_length[2]|max_length[50]');
    $this->validation->set_rules('email', 'email address', 'required|valid_email');
    $this->validation->set_rules('birth_date', 'birth date', 'required|valid_date');
    $this->validation->set_rules('password', 'password', 'required|min_length[8]');
    $this->validation->set_rules('confirm_password', 'password confirmation', 'required|matches[password]');
    $this->validation->set_rules('avatar', 'profile photo', 'is_image|square|max_size[5000]');
    
    // Run validation
    if ($this->validation->run() === true) {
        // All validation passed - process form
        $data = $this->model->get_data_from_post();
        $this->db->insert($data, 'users');
        set_flashdata('Account created successfully');
        redirect('login');
    } else {
        // Validation failed - redisplay form with errors
        $this->create();
    }
}

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.

Leave Feedback About This Page