Skip to content

magnus-eriksson/validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Validator

Simple usage

$data = [
    'foo' => 'bar',
    'lorem' => [
        'ipsum' => 'loremipsum',
    ]
];

$rules = [
    'foo'         => ['required', 'in:bar,foo'],
    'lorem.ipsum' => ['required', 'minLength:10'],
],

$v = new \Maer\Validator\Validator($data, $rules);

if ($v->isValid() === false) {
    print_r($v->getErrors());
}

Add custom rules

class MyRules extends \Maer\Validator\Sets\AbstractSet
{
    public function myRule(mixed $input): string|bool
    {
        if ($input === 'hello-world') {
            return true;
        }

        return "Input must be hello-world";
    }

    public function rules(): array
    {
        return [
            'myNewRule' => [$this, 'myRule'],
        ];
    }
}

$data = [
    'foo' => 'hello-world',
];

$rules = [
    'foo' => ['myNewRule'],
];

$v = new Validator($data, $rules);

// Add ruleset
$v->addSet(new MyRules);

About

An easy and customizable validation library

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages