Purephp Validation
is a standalone library to use the Illuminate\Validation package outside the Laravel framework.
This library is based on jeffochoa/validator-factory,
This library is customized to use with static calls, like a Laravel Facade:
$validator = Validator::make(
$data,
$rules,
$messages,
$attributes,
);
It also supports Password
rule object and File
rule object.
$validator = Validator::make(
data: $data,
rules: [
'password' => [
'required',
Password::min(8),
],
'attachment' => [
'required',
File::image(),
],
];
);
Additionally and uniquely, Instance
rule object is supported.
$validator = Validator::make(
data: [
'prop1' => new Instance([]),
'prop2' => 'Instance',
'prop3' => fn () => true,
],
rules: [
'prop1' => Instance::of(Instance::class),
'prop2' => Instance::of([
Instance::class,
Validator::class,
(fn () => true)::class,
]),
'prop3' => Instance::of('Closure'),
],
);
- PHP 8.2 or later
- Composer installed
composer require macocci7/purephp-validation
First, import autoload.php
into your code (in src/
folder) like this:
require_once __DIR__ . '/../vendor/autoload.php';
Then, create a new instance of the Illuminate\Validation\Validator
as follows:
use Macocci7\PurephpValidation\ValidatorFactory as Validator;
$validator = Validator::make(
data: [
'name' => 'foo',
'email' => 'foo@example.com',
'password' => 'Passw0rd',
],
rules: [
'name' => 'required|string|min:3|max:40',
'email' => 'required|email:rfc',
'password' => 'required|string|min:8|max:16',
],
);
Now, you can check the validation results:
if ($validator->fails()) {
var_dump($validator->errors()->message);
} else {
echo "🎊 Passed 🎉" . PHP_EOL;
}
You can learn more about writing validation rules at the Laravel Official Documentation.
Here's also an example code for basic usage: BasicUsage.php
You'll probably want to place the lang
folder somewhere else outside of vendor/
.
You can set the Translations Root Path before creating an instance of Validator
:
// Set Translations Root Path (optional)
// - The path must end with '/'.
// - 'lang/' folder must be placed under the path.
Validator::translationsRootPath(__DIR__ . '/');
You can also set the Language before creating an instance of Validator
:
// Set lang: 'en' as default (optional)
Validator::lang('ja');
Here's an example code for setting Translations Root Path and Language: SetTranslationsRootPath.php
You can validate passwords using Laravel's Password
rule object.
use Macocci7\PurephpValidation\Rules\PasswordWrapper as Password;
$validator = Validator::make(
data: [ 'password' => 'pass' ],
rules: [
'password' => [
'required',
Password::min(8)
->max(16)
// at least one letter
->letters()
// at least one uppercase
// and at least one lowercase letter
->mixedCase()
// at least one number
->numbers()
// at least one symbol
->symbols()
// not in a data leak
->uncompromised(),
],
],
);
You can learn more about Laravel's Password
rule object at the Laravel Official Document.
Here's an example code for using Password
rule object: ValidatePassword.php
You can validate files using Laravel's File
rule object.
use Illuminate\Validation\Rule;
use Macocci7\PurephpValidation\Rules\FileWrapper as File;
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
$path = __DIR__ . '/../storage/uploaded/foo.jpg';
$validator = Validator::make(
data: [
'photo' => new SymfonyFile($path),
],
rules: [
'photo' => [
'required',
File::image()
->min(10) // kilo bytes
->max(144) // kilo bytes
->dimensions(
Rule::dimensions()
->maxWidth(200) // pix
->maxHeight(300) // pix
),
],
],
);
You can learn more about Laravel's File
rule object at the Laravel Official Document.
Here's an example code for using Laravel's File
rule object: ValidateFile.php
You can validate objects using Instance
rule object. (unique feature)
By using Instance::of($class)
method as a rule, you can perform $value instanceof $class
in the validation.
Instance::of()
accepts class name(s) as an argument.
use Macocci7\PurephpValidation\Rules\Instance;
$validator = Validator::make(
data: $data,
rules: [
'prop1' => Instance::of(Instance::class),
'prop2' => Instance::of([
// Macocci7\PurephpValidation\Rules\Instance
Instance::class,
// Macocci7\PurephpValidation\ValidatorFactory
Validator::class,
// Closure
(fn () => true)::class,
]),
'prop3' => Instance::of('Closure'),
],
);
Here's an example code for using Instance
rule object: ValidateInstance.php
- BasicUsage.php
- SetTranslationsRootPath.php
- ValidatePassword.php
- ValidateFile.php
- ValidateInstance.php
Copyright 2024 macocci7