PHP code example of blakvghost / php-validator

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

    

blakvghost / php-validator example snippets


use BlakvGhost\PHPValidator\Validator;
use BlakvGhost\PHPValidator\ValidatorException;

try {
    $data = [
        'username' => 'BlakvGhost',
        'email' => '[email protected]',
        'score' => 42,
        'tags' => [
            ['name' => 'php'],
            ['name' => 'validation'],
        ],
        'friend' => [
            'email' => '[email protected]',
            'profile' => [
                'first_name' => 'Kabirou',
            ],
        ],
    ];

    $validator = new Validator($data, [
        'username' => '

$data = ['username' => 'BlakvGhost'];

$validator = new Validator(
    $data,
    ['username' => '    'string' => 'Your username must be a string',
        ],
    ]
);

// Or set the default language
$validator = new Validator($data, $rules, lang: 'en');

$data = [
    'user' => [
        'profile' => [
            'first_name' => 'Kabirou',
        ]
    ]
];

$rules = [
    'user.profile.first_name' => '

$data = [
    'products' => [
        ['name' => 'Banana'],
        ['name' => 'Apple'],
    ]
];

$rules = [
    'products.*.name' => '

[
    'string' => 'size:7',          // strlen(string) == 7
    'integer' => 'size:7',         // integer == 7
    'array' => 'size:7',           // count(array) == 7
    'file' => 'size:512',          // file size (kb) == 512
]

$data = [
    'name' => 'John',
    'age' => 30,
    'extra_field' => 'ignored'
];

$validator = new Validator($data, [
    'name' => '

namespace YourNameSpace\Rules;

use BlakvGhost\PHPValidator\Contracts\Rule;

class CustomPasswordRule implements Rule
{
    protected $field;

    public function __construct(protected array $parameters = [])
    {
    }

    public function passes(string $field, $value, array $data): bool
    {
        $this->field = $field;
        // Implement your custom validation logic here
        // Example: Check if the password equals confirm_password
        return $value === $data['confirm_password'];
    }

    public function message(): string
    {
        return "Your passwords do not match";
    }
}

use BlakvGhost\PHPValidator\Validator;
use BlakvGhost\PHPValidator\ValidatorException;
use YourNameSpace\CustomPasswordRule;

try {
    $data = [
        'password' => '42',
        'confirm_password' => '142',
    ];

    $validator = new Validator($data, [
        'password' => ['

use BlakvGhost\PHPValidator\Validator;
use BlakvGhost\PHPValidator\ValidatorException;
use BlakvGhost\PHPValidator\Mapping\RulesMaped;
use YourNameSpace\CustomPasswordRule;

// Add your rule with an alias and the full namespace
RulesMaped::addRule('c_password', CustomPasswordRule::class);

try {
    $data = [
        'password' => '42',
        'confirm_password' => '142',
    ];

    $validator = new Validator($data, [
        'password' => '
bash
composer