PHP code example of solbianca / validator

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

    

solbianca / validator example snippets


use SolBianca\Validator;

$validator = new Validator;

$validator->validate([
    'age' => ['value' => 20, 'rules' => ['ssed!';
} else {
    var_dump($validator->errors()->all();
}

$validator->addRule('sex', function ($value) {
    return in_array($value, ['male', 'female']);
})->addRuleMessage('sex', 'Field `{field}` must be male or female. Given value `{value}`.');

$validator->validate([
    'fruit' => ['value' => 'male', 'rules' => ['sex']],
]);

class SomeRule implements SolBianca\Validator\Interfaces\RuleInterface 
{
    // some code
}

// You can add as a string
$validator->addRule('sex', SomeRule::class);

// or as an object
$validator->addRule('sex', new SomeRule());

$validator->addRule('int', function ($value) {
    return (is_int($value)) && $value > 0);
})->addRuleMessage('sex', 'Field `{field}` must be integer ang greater than zero.');

$validator->validate([
    'fruit' => ['value' => 'male', 'rules' => ['sex']],
]);

$validator->addRuleMessage('

$v->addRuleMessages([
    'eld} needs to be an integer, but I found {value}.',
]);

$validator->validate([
    'username_box' => ['value' => '', 'rules' => ['

$validator->validate([
    'some_input' => ['value' => [10, 20], 'rules' => ['array']],
]);

$validator->validate([
    'some_input' => ['value' => 5, 'rules' => ['between' => [5, 10]]],
]);

$validator->validate([
    'some_input' => ['value' => true, 'rules' => ['bool']],
]);

$validator->validate([
    'some_input' => ['value' => '[email protected]', 'rules' => ['email']],
]);

$validator->validate([
    'some_input' => ['value' => 42, 'rules' => ['int']],
]);

$validator->validate([
    'some_input' => ['value' => '127.0.0.1', 'rules' => ['ip']],
]);

$validator->validate([
    'some_input' => ['value' => 1, 'rules' => ['int', 'matches' => 'other_input']],
    'other_input' => ['value' => 1, 'rules' => ['int']]
]);

$validator->validate([
    'some_input' => ['value' => 5, 'rules' => ['max' => 10]],
    'other_input' => ['value' => 0.5, 'rules' => ['max' => [1.0, 'number']]],
]);

$validator->validate([
    'some_input' => ['value' => 5, 'rules' => ['min' => 1]],
    'other_input' => ['value' => 0.5, 'rules' => ['min' => [0.0, 'number']]],
]);

$validator->validate([
    'some_input' => ['value' => '5', 'rules' => ['number']],
]);

$validator->validate([
    'some_input' => ['value' => 'bag', 'rules' => ['regex' => '/b[aeiou]g/']],
]);

$validator->validate([
    'some_input' => ['value' => true, 'rules' => ['

$validator->validate([
    'some_input' => ['value' => 'http://example.com', 'rules' => ['url']],
]);
json
{
    "olbianca/php-validator": "1.*"
    }
}