Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

GUMP

> 编程语言
Open source

A fast, flexible PHP validation class that works anywhere and validates any data.

1.2K stars0 likes1 views
WebsiteGitHub

About

A fast, flexible PHP validation class that works anywhere and validates any data.

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)

bash
composer require wixel/gump

Manual Installation

  1. Download the latest release from GitHub Releases
  2. Extract and include gump.class.php in your project:
php
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 support
    • ext-json - JSON processing
    • ext-intl - Internationalization functions
    • ext-bcmath - Arbitrary precision mathematics
    • ext-iconv - Character encoding conversion

Quick Start

Simple Validation

php
 'johndoe',
    'email'    => '[email protected]',
    '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

php
$filtered = GUMP::filter_input([
    'username' => ' JohnDoe123 ',
    'bio'      => '
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

…

File Upload Validation

php
$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

…

Available Validators

GUMP provides 76 built-in validators for comprehensive data validation:

Comprehensive Validator Reference

Essential Validators

required - The most fundamental validator

php
// Basic usage
'email' => 'required'

// Common combinations
'username' => 'required|alpha_numeric|min_len,3|max_len,20'
'password' => 'required|min_len,8|max_len,100'

between_len,min;max - String length range validation

php
// Username must be between 3-20 characters
'username' => 'between_len,3;20'

// Description between 10-500 characters
'description' => 'between_len,10;500'

// Works with Unicode characters
'title' => 'between_len,5;100'  // Handles émojis, ñ, etc. correctly

Real-World Usage Examples

User Registration Form

php
$rules = [
    'first_name' => 'required|alpha_space|min_len,2|max_len,50',
    'last_name'  => 'required|alpha_space|min_len,2|max_len,50', 
    'username'   => 'required|alpha_numeric_dash|between_len,3;20',
    'email'      => 'required|valid_email',
    'phone'      => 'phone_number',
    'website'    => 'valid_url',
    'age'        => 'required|integer|min_numeric,13|max_numeric,120',
    'password'   => 'required|min_len,8',
    'password_confirm' => 'required|equalsfield,password',
    'terms'      => 'required|boolean,strict'
];

E-commerce Product Form

php
$rules = [
    'name'        => 'required|between_len,3;100',
    'price'       => 'required|float|min_numeric,0.01',
    'category'    => 'required|contains,electronics;clothing;books;home',
    'sku'         => 'required|regex,/^[A-Z]{2}[0-9]{4}$/',
    'description' => 'required|between_len,20;2000',
    'tags'        => 'valid_array_size_greater,0|valid_array_size_lesser,11',
    'active'      => 'boolean',
    'image'       => 'required_file|extension,jpg;png;webp'
];

API Payload Validation

php
$rules = [
    'user_id'     => 'required|uuid',
    'email'       => 'required|valid_email', 
    'metadata'    => 'valid_json_string',
    'permissions' => 'valid_array_size_greater,0',
    'expires_at'  => 'date,c',  // ISO 8601 format
    'ip_address'  => 'valid_ip',
    'user_agent'  => 'max_len,500'
];

Security & Authentication Form

php
$rules = [
    'password'    => 'required|strong_password',
    'token'       => 'required|jwt_token',
    'api_key'     => 'required|hash,sha256',
    'input'       => 'no_sql_injection|no_xss',
    'timezone'    => 'timezone',
    'language'    => 'language_code'
];

Geographic & Network Validation

php
$rules = [
    'latitude'    => 'required|latitude',
    'longitude'   => 'required|longitude',
    'postal_code' => 'required|postal_code,US',
    'coordinates' => 'coordinates',
    'mac_address' => 'mac_address',
    'domain'      => 'domain_name',
    'port'        => 'port_number',
    'twitter'     => 'social_handle'
];

Content & Format Validation

php
$rules = [
    'variable_name' => 'required|snake_case',
    'functionName'  => 'required|camel_case',
    'blog_slug'     => 'required|url_slug',
    'article_body'  => 'required|word_count,min,100,max,2000',
    'color_theme'   => 'hex_color',
    'schedule_date' => 'required|future_date|business_day',
    'prime_number'  => 'prime'
];

Advanced Validation Patterns

Conditional Validation

php
// Only validate credit card if payment method is 'credit_card'
if ($input['payment_method'] === 'credit_card') {
    $rules['credit_card'] = 'required|valid_cc';
    $rules['expiry_date'] = 'required|date,m/Y';
}

File Upload with Metadata

php
$rules = [
    'title'       => 'required|between_len,3;100',
    'document'    => 'required_file|extension,pdf;doc;docx',
    'category'    => 'required|contains_list,public;private;draft',
    'tags'        => 'valid_array_size_lesser,10',
    'description' => 'max_len,1000'
];

Nested Array Validation

php
$rules = [
    'company.name'           => 'required|between_len,2;100',
    'company.email'          => 'required|valid_email',
    'employees.*.name'       => 'required|valid_name',
    'employees.*.email'      => 'required|valid_email', 
    'employees.*.age'        => 'required|integer|min_numeric,18',
    'departments.*.budget'   => 'required|float|min_numeric,0'
];

Pro Tips:

Parameter Conflicts: When using pipe (|) or semicolon (;) in validator parameters, use array format:

php
// Wrong - will break parsing
'field' => 'regex,/part|of;pattern/'

// Correct - use array format
'field' => ['regex' => '/part|of;pattern/']

Performance: Put faster validators first in chains:

php
// Good - required fails fast for empty values
'email' => 'required|valid_email|max_len,255'

// Less efficient - validates email format on empty values
'email' => 'valid_email|required|max_len,255'

Boolean Values: The boolean filter accepts various formats:

php
// All these become TRUE: '1', 1, 'true', true, 'yes', 'on'
// All these become FALSE: '0', 0, 'false', false, 'no', 'off', null, ''

Available Filters

GUMP includes 16 filters for data sanitization and transformation:

Filter Chaining Example

php
$filtered = GUMP::filter_input([
    'title'       => '  My Amazing Blog Post!!!  ',
    'description' => '
This is a description with bold text.',
    'price'       => '$19.99 USD',
    'active'      => 'yes'
], [
    'title'       => 'trim|ms_word_characters|slug',
    'description' => 'trim|sanitize_string',
    'price'       => 'sanitize_floats',
    'active'      => 'boolean'
]);

// Results:
// $filtered['title'] = 'my-amazing-blog-post'
// $filtered['description'] = 'This is a description with bold text.'
// $filtered['price'] = '19.99'
// $filtered['active'] = true

Advanced Usage

Singleton Pattern

GUMP supports a singleton pattern for cases where you want to reuse the same instance across your application:

php
// Get the singleton instance
$gump = GUMP::get_instance();

// Subsequent calls return the same instance
$sameGump = GUMP::get_instance();

// Both variables reference the same object
var_dump($gump === $sameGump); // true

Field Helper Method

The field() static method provides safe array access with a default fallback:

php
// Safely get a value from an array with a default
$username = GUMP::field('username', $_POST, 'guest');

// Returns 'guest' if 'username' key doesn't exist
$email = GUMP::field('email', $_POST); // Returns null if not set

// Useful for optional form fields
$data = [
    'name' => 'John',
    'age' => 25
];

$name = GUMP::field('name', $data);           // 'John'
$country = GUMP::field('country', $data, 'US'); // 'US' (default)

Instance Methods

…

Field Name Customization

php
// Set friendly field names for error messages
GUMP::set_field_name('usr_nm', 'Username');
GUMP::set_field_names([
    'usr_nm' => 'Username',
    'pwd'    => 'Password',
    'em'     => 'Email Address'
]);

// Now validation errors will show friendly names
$is_valid = GUMP::is_valid(['usr_nm' => ''], ['usr_nm' => 'required']);
// Error: "Username is required" (instead of "Usr nm is required")

Global Error Message Customization

php
// Set custom error messages for validators
GUMP::set_error_message('required', 'The {field} field cannot be empty');
GUMP::set_error_message('valid_email', 'Please provide a valid email for {field}');

// Set multiple custom messages
GUMP::set_error_messages([
    'required'      => 'Please fill out the {field} field',
    'min_len'       => '{field} must be at least {param} characters long',
    'valid_email'   => 'The email address in {field} is not valid'
]);

Internationalization

GUMP supports 19 languages out of the box:

Supported Languages: German (de), Greek (el), English (en), Esperanto (eo), Spanish (es), Persian (fa), French (fr), Hebrew (he), Hungarian (hu), Indonesian (id), Italian (it), Japanese (ja), Dutch (nl), Portuguese Brazil (pt-br), Russian (ru), Turkish (tr), Ukrainian (uk), Vietnamese (vi), Chinese Simplified (zh-CN)

php
// Set language during instantiation
$gump = new GUMP('es'); // Spanish
$gump = new GUMP('fr'); // French
$gump = new GUMP('de'); // German

// Validation errors will now be in the selected language
$result = $gump->validate(['email' => 'invalid'], ['email' => 'valid_email']);

Custom Validators & Filters

Adding Custom Validators

…

Adding Custom Filters

php
// Add a custom filter
GUMP::add_filter('mask_email', function($value, array $params = []) {
    $

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPphpvalidationvalidator

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言