A simple validator is a lightweight PHP validation library written in modern PHP 8. It provides a simple and expressive way to validate data using a fluent interface. The library uses Pest for unit testing.
composer require sadhakbj/validator
<?php
require_once 'vendor/autoload.php';
use Sadhakbj\Validator\Rules\Required;
use Sadhakbj\Validator\Rules\Email;
$data = [
'name' => 'Bijaya',
'age' => 25,
'email' => 'abcd',
];
$validator = new \Sadhakbj\Validator\Validator($data);
$validator->setRules([
'name' => ['required', new Required()],
'age' => [new Required(), 'max:10'],
'email' => ['required', new Email()],
]);
if ($validator->validate()) {
dd('Validation succeeded');
} else {
dd($validator->getErrors());
}
- Create a
Validator
instance with your data. - Set validation rules using the
setRules
method. - Use the
validate
method to check if the data is valid. - Access validation errors using the
getErrors
method.
// ... (Same as Quick Start)
if ($validator->validate()) {
dd('Validation succeeded');
} else {
dd($validator->getErrors());
}
In case of validation failure, getErrors
returns an associative array with field names as keys and an array of error messages as values.
Feel free to contribute by opening issues or submitting pull requests on GitHub.
This project is licensed under the MIT License - see the LICENSE file for details.