PHP code example of julienlinard / php-validator

1. Go to this page and download the library: Download julienlinard/php-validator 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/ */

    

julienlinard / php-validator example snippets




ulienLinard\Validator\Validator;

$validator = new Validator();

$data = [
    'email' => '[email protected]',
    'password' => 'password123',
    'age' => 25,
];

$rules = [
    'email' => '   // Use validated data
} else {
    $errors = $result->getErrors();
    // Handle errors
}

$rules = ['name' => '

$rules = ['email' => 'email'];

$rules = [
    'password' => 'min:8',
    'title' => 'max:100',
];

$rules = ['age' => 'numeric'];

$rules = ['website' => 'url'];

$rules = ['status' => 'in:active,inactive,pending'];

$rules = ['phone' => 'pattern:/^\+?[1-9]\d{1,14}$/'];

$rules = [
    'birthday' => 'date',                    // Any valid date format
    'created_at' => 'date:Y-m-d H:i:s',     // Specific format
];

$rules = ['is_active' => 'boolean'];

$rules = [
    'age' => 'between:18,65',        // Numeric: between 18 and 65
    'title' => 'between:5,100',      // String length: between 5 and 100 characters
];

$rules = [
    'document' => 'file',           // Any file
    'document' => 'file:10485760',  // Max 10MB (in bytes)
];

// For MIME type validation, use array format:
$rules = [
    'document' => [
        'file' => [10485760, ['application/pdf', 'application/msword']]
    ]
];

$rules = [
    'avatar' => 'image',           // Any image
    'avatar' => 'image:10485760',  // Max 10MB (in bytes)
];

// For specific image types, use array format:
$rules = [
    'avatar' => [
        'image' => [10485760, ['image/jpeg', 'image/png']]  // Max size first, then allowed types
    ]
];

$rules = [
    'code' => 'size:6',          // String: exactly 6 characters
    'file' => 'size:1024',        // File: exactly 1024 bytes
    'count' => 'size:10',        // Number: exactly 10
];

$rules = ['name' => 'alpha'];

$rules = ['username' => 'alpha_num'];

$rules = ['slug' => 'alpha_dash'];

$rules = ['password' => 'nfirmation' field to match 'password'

$rules = [
    'ip' => 'ip',        // IPv4 or IPv6
    'ip' => 'ipv4',      // IPv4 only
    'ip' => 'ipv6',      // IPv6 only
];

$rules = ['config' => 'json'];

$rules = ['id' => 'uuid'];

$rules = ['terms' => 'accepted'];

$rules = ['optional_field' => 'filled'];

$rules = [
    'start_date' => 'date|before:end_date',
    'end_date' => 'date|after:start_date',
    'birthday' => 'date|before:today',  // or 'before:2024-01-01'
];

$rules = [
    'new_password' => 'different:old_password',
    'password_confirmation' => 'same:password',
];

// Create validator with a specific locale
$validator = new Validator('en'); // English
$validator = new Validator('fr'); // French (default)
$validator = new Validator('es'); // Spanish

// Or change locale after creation
$validator = new Validator();
$validator->setLocale('en');

// Get current locale
$locale = $validator->getLocale(); // Returns 'en', 'fr', or 'es'

$validator = new Validator();
$validator->setCustomMessages([
    'email.email' => 'Please provide a valid email address',
    'password.min' => 'Password must be at least :min characters',
    'name.

$validator = new Validator();
$validator->setSanitize(true); // Default: true

$data = ['name' => '  <script>alert("xss")</script>  '];
$result = $validator->validate($data, ['name' => '

use JulienLinard\Validator\Rules\AbstractRule;
use JulienLinard\Validator\Validator;

class CustomRule extends AbstractRule
{
    public function getName(): string
    {
        return 'custom';
    }

    public function validate(mixed $value, array $params = []): bool
    {
        // Your validation logic
        return $value === 'expected';
    }

    protected function getDefaultMessage(): string
    {
        return 'The :field field is invalid.';
    }
}

$validator = new Validator();
$validator->registerRule(new CustomRule());

$rules = ['field' => 'custom'];

use JulienLinard\Core\Controller\Controller;
use JulienLinard\Core\Form\FormResult;
use JulienLinard\Validator\Validator;

class UserController extends Controller
{
    public function store()
    {
        $validator = new Validator();
        $result = $validator->validate($_POST, [
            'email' => 'r, $field));
                }
            }
            return $this->view('users/create', ['formResult' => $formResult]);
        }

        // Use validated data
        $validated = $result->getValidated();
        // ...
    }
}

$result = $validator->validate($data, $rules);

$validator->setCustomMessages([
    'email.email' => 'Invalid email',
]);

$validator->setSanitize(false);

$validator->registerRule(new CustomRule());

if ($result->isValid()) {
    // Success
}

if ($result->hasErrors()) {
    // Has errors
}

$errors = $result->getErrors();
// ['email' => ['Email is 

$emailErrors = $result->getFieldErrors('email');

$firstError = $result->getFirstError('email');

$validated = $result->getValidated();

$email = $result->getValidatedValue('email');
bash
composer