PHP code example of wixel / gump

1. Go to this page and download the library: Download wixel/gump library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wixel / gump example snippets





valid = GUMP::is_valid([
    'username' => 'johndoe',
    'email'    => '[email protected]',
    'age'      => '25'
], [
    'username' => 'ata is valid!";
} else {
    // Display validation errors
    foreach ($is_valid as $error) {
        echo "❌ " . $error . "\n";
    }
}

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

$gump = new GUMP();

// Set validation rules
$gump->validation_rules([
    'username'    => 'email',
    'phone'       => '$gump->set_fields_error_messages([
    'username' => [
        'id 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);
}

$is_valid = GUMP::is_valid(array_merge($_POST, $_FILES), [
    'profile_photo' => ' => ' Error: {$error}\n";
    }
}

$data = [
    'user' => [
        'name'  => 'John Doe',
        'email' => '[email protected]'
    ],
    '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'        => '

// Basic usage
'email' => 'sername' => 'en,100'

// 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

$rules = [
    'first_name' => 'lpha_space|min_len,2|max_len,50', 
    'username'   => 'bsite'    => 'valid_url',
    'age'        => 'lean,strict'
];

$rules = [
    'name'        => 'loat|min_numeric,0.01',
    'category'    => '=> ''active'      => 'boolean',
    'image'       => '

$rules = [
    'user_id'     => 'alid_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'
];

$rules = [
    'password'    => 'wt_token',
    'api_key'     => ' => 'timezone',
    'language'    => 'language_code'
];

$rules = [
    'latitude'    => 'ongitude',
    'postal_code' => 'ress',
    'domain'      => 'domain_name',
    'port'        => 'port_number',
    'twitter'     => 'social_handle'
];

$rules = [
    'variable_name' => 'amel_case',
    'blog_slug'     => 'lor_theme'   => 'hex_color',
    'schedule_date' => '

// Only validate credit card if payment method is 'credit_card'
if ($input['payment_method'] === 'credit_card') {
    $rules['credit_card'] = '

$rules = [
    'title'       => 'ile|extension,pdf;doc;docx',
    'category'    => '00'
];

$rules = [
    'company.name'           => 'alid_email',
    'employees.*.name'       => ''

> // ❌ Wrong - will break parsing
> 'field' => 'regex,/part|of;pattern/'
> 
> // ✅ Correct - use array format
> 'field' => ['regex' => '/part|of;pattern/']
> 

> // ✅ Good - equired|valid_email|max_len,255'
> 
> // ❌ Less efficient - validates email format on empty values
> 'email' => 'valid_email|

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

$filtered = GUMP::filter_input([
    'title'       => '  My Amazing Blog Post!!!  ',
    'description' => '<script>alert("xss")</script>This is a description with <b>bold</b> 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

$gump = new GUMP('en'); // Set language

// Validate data without filtering
$validation_result = $gump->validate($_POST, [
    'email' => 'ata (UTF-8 conversion)
$sanitized = $gump->sanitize($_POST, ['allowed_field1', 'allowed_field2']);

// Get detailed error information
if ($gump->errors()) {
    $readable_errors = $gump->get_readable_errors(); // HTML formatted
    $simple_errors = $gump->get_errors_array();      // Field => message array
}

// 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' => '

// Set custom error messages for validators
GUMP::set_error_message(' for {field}');

// Set multiple custom messages
GUMP::set_error_messages([
    '

// 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']);

// Add a custom validator with callback
GUMP::add_validator('strong_password', function($field, array $input, array $params, $value) {
    // Must contain at least 1 uppercase, 1 lowercase, 1 number, and 1 special char
    return preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]/', $value);
}, 'The {field} must be a strong password with uppercase, lowercase, number and special character.');

// Usage
$is_valid = GUMP::is_valid(['password' => 'weak'], ['password' => 'strong_password']);

// Check if validator exists
if (GUMP::has_validator('strong_password')) {
    echo "Custom validator is available!";
}

// Add a custom filter
GUMP::add_filter('mask_email', function($value, array $params = []) {
    $parts = explode('@', $value);
    if (count($parts) === 2) {
        $username = substr($parts[0], 0, 2) . str_repeat('*', strlen($parts[0]) - 2);
        return $username . '@' . $parts[1];
    }
    return $value;
});

// Usage
$filtered = GUMP::filter_input(['email' => '[email protected]'], ['email' => 'mask_email']);
// Result: 'jo***@example.com'

// Check if filter exists
if (GUMP::has_filter('mask_email')) {
    echo "Custom filter is available!";
}

class CustomGUMP extends GUMP
{
    // Custom validator method (prefix with 'validate_')
    protected function validate_is_even($field, array $input, array $params = [], $value = null)
    {
        return is_numeric($value) && ($value % 2 == 0);
    }
    
    // Custom filter method (prefix with 'filter_')
    protected function filter_add_prefix($value, array $params = [])
    {
        $prefix = isset($params[0]) ? $params[0] : 'PREFIX_';
        return $prefix . $value;
    }
}

$custom_gump = new CustomGUMP();

// Use custom validator
$result = $custom_gump->validate(['number' => 5], ['number' => 'is_even']);

// Use custom filter  
$filtered = $custom_gump->filter(['name' => 'John'], ['name' => 'add_prefix,MR_']);
// Result: 'MR_John'

// Default configuration
GUMP::$rules_delimiter = '|';                    // Separates rules: 'ules_parameters_arrays_delimiter = ';'; // Separates array items: 'contains,a;b;c'

// Custom configuration example
GUMP::$rules_delimiter = '&';                    // '

// Characters that will be replaced with spaces in field names for error messages
GUMP::$field_chars_to_spaces = ['_', '-', '.'];

// 'user_name' becomes 'User Name' in error messages
// 'first-name' becomes 'First Name' in error messages