solbianca/validator

Easy to use, highly customisable PHP validator.

v1.0 2017-12-23 16:47 UTC

This package is auto-updated.

Last update: 2024-04-29 03:27:15 UTC


README

This is an easy to use and customisable PHP validator.

Note: This package is under development and not recommended for production.

Build Status Scrutinizer Code Quality Maintainability

Installing

Install via composer

composer require solbianca/validator

or add to composer.json

{
    "require": {
        "solbianca/php-validator": "1.*"
    }
}

Basic usage

use SolBianca\Validator;

$validator = new Validator;

$validator->validate([
    'age' => ['value' => 20, 'rules' => ['required', 'int']],
    'name' => ['value' => 'John Doe', 'rules' => ['required']],
]);

if($validator->passed()) {
    echo 'Validation passed!';
} else {
    var_dump($validator->errors()->all();
}

Adding custom rules

Adding custom rules is simple. It can be any callable or object which implement SolBianca\Validator\Interfaces\RuleInterface

If the callable returns false, the rule fails.

$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());

Rewrite rules

Validator have useful default rules as int, required and many more. You can rewrite any rule by your own.

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

Adding custom error messages

You can add custom error messages for any rule

$validator->addRuleMessage('required', 'You better fill in the {field} field, or else.');

Adding rule messages in bulk

$v->addRuleMessages([
    'required' => 'You better fill in the {field} field, or else.',
    'int'      => 'The {field} needs to be an integer, but I found {value}.',
]);

Using Field Aliases

Field Aliases helps you format any error messages without showing weird form names or the need to create a custom error.

$validator->validate([
    'username_box' => ['value' => '', 'rules' => ['required'], 'alias' => 'Username']
]);

// Error output: "Field `Username` is required."

Rules

Array

If the value is an array.

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

Between

Checks if the value is within the intervals defined. This check is inclusive, so 5 is between 5 and 10.

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

Bool

If the value is a boolean.

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

Email

If the value is a valid email.

$validator->validate([
    'some_input' => ['value' => 'mail@example.com', 'rules' => ['email']],
]);

Int

If the value is an integer, including numbers within strings. 1 and '1' are both classed as integers.

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

Ip

If the value is a valid IP address.

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

Matches

Checks if one given input matches the other. For example, checking if password matches password_confirm.

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

Max

Check if string length is less than or equal to given int. To check the size of a number, pass the optional number option.

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

Mix

Check if string length is greater than or equal to given int. To check the size of a number, pass the optional number option.

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

Number

If the value is a number, including numbers within strings.

Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal (e.g. 0xf4c3b00c), Binary (e.g. 0b10100111001), Octal (e.g. 0777) notation is allowed too but only without sign, decimal and exponential part.

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

Regex

If the given input has a match for the regular expression given.

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

Required

If the value is present.

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

Url

If the value is formatted as a valid URL.

$validator->validate([
    'some_input' => ['value' => 'http://example.com', 'rules' => ['url']],
]);