GUMP
A fast, flexible PHP validation class that works anywhere and validates any data.
Install / Use
/learn @Wixel/GUMPREADME
GUMP - A Fast PHP Data Validation & Filtering Library
Overview
GUMP is a standalone PHP data validation and filtering library that makes validating any data easy and painless without the reliance on a framework. GUMP has been serving the PHP community since 2013 and is trusted by thousands of developers worldwide.
Key Features
- Zero Dependencies - Pure PHP, no external dependencies required
- 19 Languages - Built-in internationalization support
- High Performance - Lightweight and fast validation processing
- Extensible - Easy to add custom validators and filters
- 76 Validators - Comprehensive set of validation rules out of the box
- Security Focused - Built-in XSS protection and data sanitization
- Framework Agnostic - Works with any PHP project or framework
- Modern PHP - Supports PHP 7.1 to 8.4+
Table of Contents
- Installation
- Requirements
- Quick Start
- Usage Examples
- Available Validators
- Available Filters
- Advanced Usage
- Internationalization
- Custom Validators & Filters
- Configuration
- Testing
- Contributing
- Security
- Support
- License
Installation
Via Composer (Recommended)
composer require wixel/gump
Manual Installation
- Download the latest release from GitHub Releases
- Extract and include
gump.class.phpin your project:
require_once 'path/to/gump.class.php';
Requirements
- PHP: 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, 8.2, 8.3, 8.4+
- Extensions:
ext-mbstring- Multibyte string supportext-json- JSON processingext-intl- Internationalization functionsext-bcmath- Arbitrary precision mathematicsext-iconv- Character encoding conversion
Quick Start
Simple Validation
<?php
require_once 'vendor/autoload.php';
$is_valid = GUMP::is_valid([
'username' => 'johndoe',
'email' => 'john@example.com',
'age' => '25'
], [
'username' => 'required|alpha_numeric|min_len,3',
'email' => 'required|valid_email',
'age' => 'required|integer|min_numeric,18'
]);
if ($is_valid === true) {
echo "All data is valid!";
} else {
// Display validation errors
foreach ($is_valid as $error) {
echo "Error: " . $error . "\n";
}
}
Simple Filtering
$filtered = GUMP::filter_input([
'username' => ' JohnDoe123 ',
'bio' => '<script>alert("xss")</script>Clean bio text'
], [
'username' => 'trim|lower_case',
'bio' => 'trim|sanitize_string'
]);
// Result:
// $filtered['username'] = 'johndoe123'
// $filtered['bio'] = 'Clean bio text'
Usage Examples
Basic Validation with Custom Error Messages
$gump = new GUMP();
// Set validation rules
$gump->validation_rules([
'username' => 'required|alpha_numeric|max_len,100|min_len,6',
'password' => 'required|max_len,100|min_len,8',
'email' => 'required|valid_email',
'phone' => 'required|phone_number',
'website' => 'valid_url',
'birthday' => 'required|date,Y-m-d|min_age,18'
]);
// Set custom error messages
$gump->set_fields_error_messages([
'username' => [
'required' => 'Please enter a username',
'min_len' => 'Username must be at least 6 characters'
],
'email' => [
'required' => 'Email address is required',
'valid_email' => 'Please enter a valid email address'
]
]);
// Set filtering rules
$gump->filter_rules([
'username' => 'trim|sanitize_string',
'email' => 'trim|sanitize_email',
'phone' => 'trim',
'website' => 'trim'
]);
$validated_data = $gump->run($_POST);
if ($gump->errors()) {
// Handle validation errors
$errors = $gump->get_readable_errors();
foreach ($errors as $error) {
echo "<div class='error'>{$error}</div>";
}
} else {
// Process validated and filtered data
echo "User registered successfully!";
var_dump($validated_data);
}
File Upload Validation
$is_valid = GUMP::is_valid(array_merge($_POST, $_FILES), [
'profile_photo' => 'required_file|extension,jpg;jpeg;png;gif',
'document' => 'required_file|extension,pdf;doc;docx',
'username' => 'required|alpha_numeric'
]);
if ($is_valid !== true) {
foreach ($is_valid as $error) {
echo "Upload Error: {$error}\n";
}
}
Array and Nested Field Validation
$data = [
'user' => [
'name' => 'John Doe',
'email' => 'john@example.com'
],
'products' => [
['name' => 'Product 1', 'price' => 19.99],
['name' => 'Product 2', 'price' => 29.99]
],
'tags' => ['php', 'validation', 'security']
];
$is_valid = GUMP::is_valid($data, [
'user.name' => 'required|valid_name',
'user.email' => 'required|valid_email',
'products.*.name' => 'required|min_len,3',
'products.*.price' => 'required|float|min_numeric,0',
'tags' => 'required|valid_array_size_greater,0'
]);
Available Validators
GUMP provides 76 built-in validators for comprehensive data validation:
<div id="available_validators">| Rule | Description | |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | required | Ensures the specified key value exists and is not empty (not null, not empty string, not empty array). | | contains,one;two;use array format if one of the values contains semicolons | Verify that a value is contained within the pre-defined value set. | | contains_list,value1;value2 | Verify that a value is contained within the pre-defined value set. Error message will NOT show the list of possible values. | | doesnt_contain_list,value1;value2 | Verify that a value is contained within the pre-defined value set. Error message will NOT show the list of possible values. | | boolean,strict | Determine if the provided value is a valid boolean. Returns true for: yes/no, on/off, 1/0, true/false. In strict mode (optional) only true/false will be valid which you can combine with boolean filter. | | valid_email | Determine if the provided email has valid format. | | max_len,240 | Determine if the provided value length is less or equal to a specific value. | | min_len,4 | Determine if the provided value length is more or equal to a specific value. | | exact_len,5 | Determine if the provided value length matches a specific value. | | between_len,3;11 | Determine if the provided value length is between min and max values. | | alpha | Determine if the provided value contains only alpha characters. | | alpha_numeric | Determine if the provided value contains only alpha-numeric characters.
