PHP code example of fi1a / validation

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

    

fi1a / validation example snippets


use Fi1a\Validation\Validator;

$validator = new Validator();

$validation = $validator->make($_POST + $_FILES, [
    'id' => '")',
    'tags' => 'array|}

use Fi1a\Validation\AllOf;

$result = AllOf::create()->ors()->join("; "); // "Значение не является целым числом"
}

use Fi1a\Validation\OneOf;

$chain = OneOf::create()->integer()->boolean();

$result = $chain->validate(true);
echo $result->isSuccess(); // true

$result = $chain->validate(10);
echo $result->isSuccess(); // true

$result = $chain->validate(null);
echo $result->isSuccess(); // false
echo $result->getErrors()->join("; "); // Значение не является целым числом; Значение должно быть логическим

use Fi1a\Validation\AllOf;
use Fi1a\Validation\OneOf;

$chain = OneOf::create(
    AllOf::create()->numeric()->min(10),
    AllOf::create()->alpha()->minLength(2)
);

$result = $chain->validate(20);
echo $result->isSuccess(); // true

$result = $chain->validate('abc');
echo $result->isSuccess(); // true

$result = $chain->validate('a');
echo $result->isSuccess(); // false
echo $result->getErrors()->join("; "); // Значение не является числом; Значение должно быть минимум 10; Длина значения должна быть больше 2

class DTO
{
    public $propertyA = 100;

    public $propertyB = 'string';

    public $propertyC;

    public $propertyD = true;

    public function getPropertyD(): bool
    {
        return $this->propertyD;
    }
}

use Fi1a\Validation\Validator;

$validator = new Validator();

$validation = $validator->make(
    new DTO(),
    [
        'propertyA' => '

use Fi1a\Validation\Validator;

$validator = new Validator();

$validation = $validator->make($_POST + $_FILES, [
    'firstName' => 'sult = $validation->validate();

if (!$result->isSuccess()) {
    echo $result->getErrors()->join("; "); // Значение "Имя" является обязательным; Значение "Фамилия" является обязательным
}

$validation->setTitle('firstName', null);
$validation->setTitle('lastName', null);

$result = $validation->validate();

if (!$result->isSuccess()) {
    echo $result->getErrors()->join("; "); // Значение является обязательным; Значение является обязательным
}

use Fi1a\Validation\Validator;

$validator = new Validator();

$validation = $validator->make($_POST + $_FILES, [
    'id' => 'getErrors(); // \Fi1a\Validation\Errors
    echo $errors->firstOfAll()->join('; ');
}

use Fi1a\Validation\Validator;

$validator = new Validator();

$validation = $validator->make($_POST + $_FILES, [
    'id' => 'getValues(); // \Fi1a\Validation\ResultValues|\Fi1a\Validation\Value[]
    $values->getInvalid(); // \Fi1a\Validation\ResultValues|\Fi1a\Validation\Value[]
    $values->getValid(); // \Fi1a\Validation\ResultValues|\Fi1a\Validation\Value[]
}

use Fi1a\Validation\AllOf;
use Fi1a\Validation\On;
use Fi1a\Validation\Validator;

$validator = new Validator();
$values = [
    'key1' => [1, 2, 3],
];
$rules = [
    'key1' => 'array',
    new On('key1', AllOf::create()->minCount(1), 'create'),
    new On('key1', AllOf::create()->minCount(4), 'update'),
    'key1:*' => '

use Fi1a\Validation\AbstractRuleSet;

/**
 * Набор правил
 */
class UserRuleSet extends AbstractRuleSet
{
    /**
     * @inheritDoc
     */
    public function init(): bool
    {
        $this->fields('id', 'email', 'tags', 'tags:*:id')
            ->on('create', 'copy')
            ->allOf()
            ->fields('tags:*:id')->allOf()->numeric();
        if ($this->getValue('password')->isPresence() || $this->getScenario() === 'create') {
            $this->fields('password')->allOf()->ags' => 'тег',
        ]);
    }
}

use Fi1a\Validation\Validator;

$validator = new Validator();

$validation = $validator->make(new UserRuleSet($_POST + $_FILES), [], [], [], 'create');

$result = $validation->validate();

if (!$result->isSuccess()) {
    echo $result->getErrors()->join("\n");
}

use Fi1a\Validation\AllOf;
use Fi1a\Validation\Presence\WhenNotNull;

$chain = AllOf::create()->boolean(new WhenNotNull());

$chain->validate(null)->isSuccess(); // true
$chain->validate(true)->isSuccess(); // true
$chain->validate('not-boolean')->isSuccess(); // false

use Fi1a\Validation\Presence\WhenNotNull;
use Fi1a\Validation\Validator;

$validator = new Validator();

$validation = $validator->make(
    [
        'array' => [null, 2, 3],
    ],
    [
        'array' => 'array|minCount(1)',
        'array:*' => 'integer',
    ]
);

$validation->setPresence(new WhenNotNull());

$result = $validation->validate();
$result->isSuccess(); // true

use Fi1a\Validation\AllOf;

AllOf::create()->alphaNumeric()->validate('123abc')->isSuccess(); // true
AllOf::create()->alphaNumeric()->validate('abc 123')->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->alpha()->validate('abc')->isSuccess(); // true
AllOf::create()->alpha()->validate('abc100')->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->array()->validate([1, 2, 3])->isSuccess(); // true
AllOf::create()->array()->validate(false)->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->betweenCount(2, 5)->validate([1, 2, 3])->isSuccess(); // true
AllOf::create()->betweenCount(2, 5)->validate(3000000)->isSuccess(); // false
AllOf::create()->betweenCount(2, 5)->validate([1,])->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()
    ->betweenDate('10.10.2022 10:10:10', '12.10.2022 10:10:10')
    ->validate('11.10.2022 10:10:10')
    ->isSuccess(); // true

AllOf::create()
    ->betweenDate('10.10.2022 10:10:10', '12.10.2022 10:10:10')
    ->validate('10.10.2022 09:00:00')
    ->isSuccess(); // false

AllOf::create()
    ->betweenDate('10.10.2022', '12.10.2022', 'd.m.Y')
    ->validate('11.10.2022')
    ->isSuccess(); // true

use Fi1a\Validation\AllOf;

AllOf::create()->betweenLength(2, 5)->validate(150)->isSuccess(); // true
AllOf::create()->betweenLength(2, 5)->validate(3000000)->isSuccess(); // false
AllOf::create()->betweenLength(2, 5)->validate('abc def gh')->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->between(100, 200)->validate(150)->isSuccess(); // true
AllOf::create()->between(100, 200)->validate(300)->isSuccess(); // false
AllOf::create()->between(100, 200)->validate('abc')->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->boolean()->validate(true)->isSuccess(); // true
AllOf::create()->boolean()->validate(false)->isSuccess(); // true
AllOf::create()->boolean()->validate('TRUE')->isSuccess(); // true
AllOf::create()->boolean()->validate('FALSE')->isSuccess(); // true
AllOf::create()->boolean()->validate('0')->isSuccess(); // true
AllOf::create()->boolean()->validate('1')->isSuccess(); // true
AllOf::create()->boolean()->validate(0)->isSuccess(); // true
AllOf::create()->boolean()->validate(1)->isSuccess(); // true
AllOf::create()->boolean()->validate('Y')->isSuccess(); // true
AllOf::create()->boolean()->validate('N')->isSuccess(); // true
AllOf::create()->boolean()->validate(100)->isSuccess(); // false
AllOf::create()->boolean()->validate('abc')->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->date()->validate('10.10.2022')->isSuccess(); // true
AllOf::create()->date('d')->validate('10.10.2022')->isSuccess(); // false
AllOf::create()->date('d m, Y')->validate('10 10, 2022')->isSuccess(); // true
AllOf::create()->date()->validate('abc')->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->email()->validate('[email protected]')->isSuccess(); // true
AllOf::create()->email()->validate('foo')->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->equalDate('10.10.2022 10:10:10')->validate('10.10.2022 10:10:10')->isSuccess(); // true
AllOf::create()->equalDate('10.10.2022 10:10:10')->validate('10.10.2022 09:00:00')->isSuccess(); // false
AllOf::create()->equalDate('10.10.2022', 'd.m.Y')->validate('10.10.2022')->isSuccess(); // true

use Fi1a\Validation\AllOf;

AllOf::create()->equal(100)->validate(100)->isSuccess(); // true
AllOf::create()->equal(100)->validate(200)->isSuccess(); // false

use Fi1a\Validation\Validator;

$validator = new Validator();

$validation = $validator->make($_POST + $_FILES, [
    'photo' => '

use Fi1a\Validation\Validator;

$validator = new Validator();
$validation = $validator->make(
    [
        'columns' => [
            [
                'foo' => null,
            ],
            [
                'foo' => [
                    'bar' => 'baz'
                ],
            ],
        ],
    ],
    [
        'columns' => AllOf::create()->array(),
        'columns:*:foo' => AllOf::create()->generic(['bar' => '()->generic(['bar' => '

use Fi1a\Validation\AllOf;

AllOf::create()->url()->validate('https://domain.ru/path/')->isSuccess(); // true
AllOf::create()->url()->validate('https')->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->in(1, 2, 3)->validate(1)->isSuccess(); // true
AllOf::create()->in(1, 2, 3)->validate(100.1)->isSuccess(); // false
AllOf::create()->in('camelCase', 'UPPERCASE')->validate('uppercase')->isSuccess(); // true

use Fi1a\Validation\AllOf;

AllOf::create()->integer()->validate(1)->isSuccess(); // true
AllOf::create()->integer()->validate(100.1)->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->json()->validate(json_encode([1, 2, 3]))->isSuccess(); // true
AllOf::create()->json()->validate('{')->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->maxCount(2)->validate([1,])->isSuccess(); // true
AllOf::create()->maxCount(2)->validate(100)->isSuccess(); // false
AllOf::create()->maxCount(2)->validate([1, 2, 3])->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->maxDate('10.10.2022 11:10:10')->validate('10.10.2022 10:10:10')->isSuccess(); // true
AllOf::create()->maxDate('10.10.2022 10:10:10')->validate('10.10.2022 12:00:00')->isSuccess(); // false
AllOf::create()->maxDate('10.10.2022', 'd.m.Y')->validate('09.10.2022')->isSuccess(); // true

use Fi1a\Validation\AllOf;

AllOf::create()->maxLength(5)->validate('123')->isSuccess(); // true
AllOf::create()->maxLength(5)->validate(123)->isSuccess(); // true
AllOf::create()->maxLength(5)->validate(1000000)->isSuccess(); // false
AllOf::create()->maxLength(5)->validate('abc def h')->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->max(100)->validate(50)->isSuccess(); // true
AllOf::create()->max(200)->validate(300)->isSuccess(); // false
AllOf::create()->max(200)->validate('abc')->isSuccess(); // false

use Fi1a\Validation\Validator;

$validator = new Validator();

$validation = $validator->make($_POST + $_FILES, [
    'photo' => 'fileSize("0", "1MB")|mime("jpeg", "png")',
]);

$result = $validation->validate();

if (!$result->isSuccess()) {
    echo $result->getErrors()->join("\n");
}

use Fi1a\Validation\AllOf;

AllOf::create()->minCount(2)->validate([1, 2, 3])->isSuccess(); // true
AllOf::create()->minCount(2)->validate(100)->isSuccess(); // false
AllOf::create()->minCount(2)->validate([1])->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->minDate('10.10.2022 10:10:10')->validate('10.10.2022 10:10:10')->isSuccess(); // true
AllOf::create()->minDate('10.10.2022 10:10:10')->validate('10.10.2022 09:00:00')->isSuccess(); // false
AllOf::create()->minDate('10.10.2022', 'd.m.Y')->validate('10.10.2022')->isSuccess(); // true

use Fi1a\Validation\AllOf;

AllOf::create()->minLength(5)->validate('123456')->isSuccess(); // true
AllOf::create()->minLength(5)->validate(123456)->isSuccess(); // true
AllOf::create()->minLength(5)->validate(100)->isSuccess(); // false
AllOf::create()->minLength(5)->validate('abc')->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->min(100)->validate(200)->isSuccess(); // true
AllOf::create()->min(200)->validate(100)->isSuccess(); // false
AllOf::create()->min(200)->validate('abc')->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->notIn(1, 2, 3)->validate(4)->isSuccess(); // true
AllOf::create()->notIn(1, 2, 3)->validate(2)->isSuccess(); // false
AllOf::create()->notIn('camelCase', 'UPPERCASE')->validate('uppercase')->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->null()->validate(null)->isSuccess(); // true
AllOf::create()->null()->validate(false)->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->numeric()->validate(1)->isSuccess(); // true
AllOf::create()->numeric()->validate(false)->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->regex('/[0-9]/mui')->validate(200)->isSuccess(); // true
AllOf::create()->regex('/[0-9]/mui')->validate('abc')->isSuccess(); // false

use Fi1a\Validation\Validator;

$validator = new Validator();
$validation = $validator->make(['foo' => true], ['foo' => 'ess(); // false

$validation->setValues([]);
$validation->validate()->isSuccess(); // true

use Fi1a\Validation\AllOf;

AllOf::create()->>isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()
    -> => 'bar'], 'baz' => 'baz'], 'baz')
    ->isSuccess(); // true
                
AllOf::create()
    -> => null], 'baz' => null], 'baz')
    ->isSuccess(); // true

AllOf::create()
    -> => 'bar'], 'baz' => null], 'baz')
    ->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->same('field1')->validate(200)->isSuccess(); // false
AllOf::create()->same('bar')->validate(['foo' => 200, 'bar' => 200], 'foo')->isSuccess(); // true

use Fi1a\Validation\AllOf;

AllOf::create()->strictIn(1, 2, 3)->validate(1)->isSuccess(); // true
AllOf::create()->strictIn(1, 2, 3)->validate(100.1)->isSuccess(); // false
AllOf::create()->strictIn('camelCase', 'UPPERCASE')->validate('uppercase')->isSuccess(); // false

use Fi1a\Validation\AllOf;

AllOf::create()->strictNotIn(1, 2, 3)->validate(4)->isSuccess(); // true
AllOf::create()->strictNotIn(1, 2, 3)->validate(2)->isSuccess(); // false
AllOf::create()->strictNotIn('camelCase', 'UPPERCASE')->validate('uppercase')->isSuccess(); // true

use Fi1a\Validation\AllOf;

AllOf::create()->string()->validate('foo')->isSuccess(); // true
AllOf::create()->string()->validate(false)->isSuccess(); // false

use \Fi1a\Validation\Rule\AbstractRule;
use \Fi1a\Validation\ValueInterface;

/**
 * Проверка на уникальное значение
 */
class UniqueRule extends AbstractRule
{
    /**
     * @var Bitrix\Main\ORM\Data\DataManager
     */
    private $className;

    /**
     * @var string
     */
    private $column;

    /**
     * @var int|null
     */
    private $notId;

    /**
     * Конструктор
     */
    public function __construct(string $className, string $column, ?int $notId = null, ?WhenPresenceInterface $presence = null)
    {
        $this->className = $className;
        $this->column = $column;
        $this->notId = $notId;
        parent::__construct($presence);
    }

    /**
     * @inheritDoc
     */
    public function validate(ValueInterface $value): bool
    {
        if (!$value->isPresence()) {
            return true;
        }
        $filter = [
            $this->column => $value->getValue(),
        ];
        if ($this->notId) {
            $filter['!ID'] = $this->notId;
        }
        $success = $this->className::getCount($filter) === 0;

        if (!$success) {
            $this->addMessage('Значение {{if(name)}}"{{name}}" {{endif}}не является уникальным', 'unique');
        }

        return $success;
    }

    /**
     * @inheritDoc
     */
    public static function getRuleName(): string
    {
        return 'unique';
    }
}

\Fi1a\Validation\Validator::addRule(UniqueRule::class);

use \Fi1a\Validation\AllOf;
use \Bitrix\Main\UserTable;

$unique = AllOf::create()->unique(UserTable::class, 'LOGIN');
$unique->validate('admin')->isSuccess(); // false
$unique->validate('user')->isSuccess(); // true