Solo Validator is a lightweight, standalone PHP validation library designed for simplicity and flexibility. It provides essential validation rules out of the box, supports custom rules and messages, and integrates seamlessly into any PHP project.
- Basic Validation Rules: Includes
required
,email
,phone
,min
,max
,length
, and more - Custom Rules: Extend validation with your own rules
- Custom Error Messages: Override default messages globally or per validation
- Parameterized Rules: Define rules with parameters (e.g.,
min:8
) - Placeholder Support: Dynamic messages with
:field
and:param
placeholders - PSR-4 Compliant: Modern autoloading structure
- Comprehensive Testing: Full test coverage with PHPUnit
- Code Standards: PSR-12 compliant code
Install via Composer:
composer require solophp/validator
<?php
use Solo\Validator\Validator;
$validator = new Validator();
$data = [
'email' => 'user@example.com',
'username' => 'john_doe',
'pin_code' => '1234',
'age' => 25,
];
$rules = [
'email' => 'required|email',
'username' => 'required|min:3|max:20',
'pin_code' => 'required|length:4',
'age' => 'integer|min_value:18',
];
$errors = $validator->validate($data, $rules);
if ($validator->fails()) {
print_r($validator->errors());
} else {
echo "Validation passed!";
}
- required: The field must not be empty
- email: The field must be a valid email address
- phone: The field must be a valid phone number
- length:value: The field must be exactly
value
characters long - min:value: The field must have a minimum length of
value
- max:value: The field must not exceed
value
in length - filled: The field must not be empty
- integer: The field must be an integer
- string: The field must be a string
- regex: The field must match the provided regex pattern
- numeric: The field must be a number
- array: The field must be an array
- min_value:value: The field must be at least
value
- max_value:value: The field must not exceed
value
- nullable: The field can be null or empty
$rules = [
'username' => 'required|min:3|max:30',
'pin_code' => 'required|length:4',
'age' => 'required|integer|min_value:18',
'email' => 'required|email',
'phone' => 'phone:US',
'tags' => 'array',
'price' => 'numeric|min_value:0|max_value:1000'
];
Add your own validation logic using addCustomRule()
:
$validator->addCustomRule('even', function ($value, $param, $data) {
return (int)$value % 2 === 0;
});
// Usage
$rules = ['number' => 'even'];
$messages = ['number.even' => 'The number must be even.'];
Override default messages globally or during validation:
// Global messages
$messages = [
'required' => 'Custom required message.',
'email.email' => 'Invalid email format.'
];
$validator = new Validator($messages);
// Per-validation messages
$errors = $validator->validate($data, $rules, [
'password.min' => 'Password must be at least 8 characters.'
]);
fails()
: Check if validation failederrors()
: Get all validation errorspassed()
: Check if validation succeeded
if ($validator->fails()) {
foreach ($validator->errors() as $field => $messages) {
echo "$field: " . implode(', ', $messages);
}
}
Use :field
and :param
in messages for dynamic content:
// Default message for 'min' rule:
'The :field must be at least :param characters.'
// Becomes:
'The password must be at least 8 characters.'
composer test
Check code standards:
composer cs
Fix code standards:
composer cs-fix
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License. See LICENSE for details.