PHP code example of macocci7 / purephp-validation

1. Go to this page and download the library: Download macocci7/purephp-validation 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/ */

    

macocci7 / purephp-validation example snippets


$validator = Validator::make(
    $data,
    $rules,
    $messages,
    $attributes,
);

$validator = Validator::make(
    data: $data,
    rules: [
        'password' => [
            '       File::image(),
        ],
    ];
);

$validator = Validator::make(
    data: [
        'prop1' => new Instance([]),
        'prop2' => 'Instance',
        'prop3' => fn () => true,
    ],
    rules: [
        'prop1' => Instance::of(Instance::class),
        'prop2' => Instance::of([
            Instance::class,
            Validator::class,
            (fn () => true)::class,
        ]),
        'prop3' => Instance::of('Closure'),
    ],
);



use Macocci7\PurephpValidation\ValidatorFactory as Validator;

$validator = Validator::make(
    data: [
        'name' => 'foo',
        'email' => '[email protected]',
        'password' => 'Passw0rd',
    ],
    rules: [
        'name' => '

if ($validator->fails()) {
    var_dump($validator->errors()->message);
} else {
    echo "🎊 Passed 🎉" . PHP_EOL;
}

// Set Translations Root Path (optional)
// - The path must end with '/'.
// - 'lang/' folder must be placed under the path.
Validator::translationsRootPath(__DIR__ . '/');

// Set lang: 'en' as default (optional)
Validator::lang('ja');

use Macocci7\PurephpValidation\Rules\PasswordWrapper as Password;

$validator = Validator::make(
    data: [ 'password' => 'pass' ],
    rules: [
        'password' => [
            ' and at least one lowercase letter
                ->mixedCase()
                // at least one number
                ->numbers()
                // at least one symbol
                ->symbols()
                // not in a data leak
                ->uncompromised(),
        ],
    ],
);

use Illuminate\Validation\Rule;
use Macocci7\PurephpValidation\Rules\FileWrapper as File;
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;

$path = __DIR__ . '/../storage/uploaded/foo.jpg';

$validator = Validator::make(
    data: [
        'photo' => new SymfonyFile($path),
    ],
    rules: [
        'photo' => [
            '

use Macocci7\PurephpValidation\Rules\Instance;

$validator = Validator::make(
    data: $data,
    rules: [
        'prop1' => Instance::of(Instance::class),
        'prop2' => Instance::of([
            // Macocci7\PurephpValidation\Rules\Instance
            Instance::class,
            // Macocci7\PurephpValidation\ValidatorFactory
            Validator::class,
            // Closure
            (fn () => true)::class,
        ]),
        'prop3' => Instance::of('Closure'),
    ],
);