- Validify
Validify is a lightweight and extensible PHP validation library that simplifies validating user inputs. Whether you're building an API, a form, or any application requiring data validation, Validify provides a powerful and intuitive way to handle it.
- Simple API: Easy-to-use API for defining and executing validation rules.
- Extensible: Define custom validation rules to fit specific use cases.
- Pre-Validation Callbacks: Execute custom logic before validation.
- Conditional Validation: Dynamically apply rules based on conditions.
- Nested Data Support: Validate complex data structures using dot notation.
- Comprehensive Built-in Rules: Includes a wide range of predefined validation rules.
To install the package, use Composer:
composer require mousav1/validify
Here is an example of simple validation using Validify:
use Mousav1\Validify\Validator;
$data = [
'username' => 'john_doe',
'email' => '[email protected]',
'age' => 25,
];
$validator = new Validator($data, [
'username' => ['required', 'alpha'],
'email' => ['required', 'email'],
'age' => ['required', 'numeric', 'min:18'],
]);
if (!$validator->validate()) {
print_r($validator->getErrors());
}
Define validation rules using a fluent interface for better readability:
use Mousav1\Validify\Validator;
$data = [
'name' => 'prefix_name',
];
$validator = new Validator($data);
$validator->field('name')
->required()
->applyRules();
if (!$validator->validate()) {
print_r($validator->getErrors());
}
Extend Validify with your own custom rules:
use Mousav1\Validify\Validator;
Validator::extend('even', function () {
return new class extends \Mousav1\Validify\Rules\Rule {
public function passes($field, $value, array $data): bool
{
return $value % 2 === 0;
}
public function name(): string
{
return 'even';
}
public function message($field): string
{
return "{$field} must be an even number.";
}
};
});
$data = ['number' => 3];
$validator = new Validator($data, [
'number' => ['even']
]);
if (!$validator->validate()) {
print_r($validator->getErrors());
}
Apply rules dynamically based on conditions:
$data = [
'age' => 20,
'license' => ''
];
$validator = new Validator($data);
$validator->addConditionalRule('license', ['required'], function ($data) {
return $data['age'] > 18;
});
if (!$validator->validate()) {
print_r($validator->getErrors());
}
Validate date and time fields using predefined rules:
$data = [
'birthdate' => '2024-09-01',
];
$validator = new Validator($data, [
'birthdate' => ['required', 'date_format:Y-m-d'],
]);
if (!$validator->validate()) {
print_r($validator->getErrors());
}
$data = [
'start_date' => '2024-01-01',
'end_date' => '2024-02-01',
];
$validator = new Validator($data, [
'end_date' => ['required', 'date_format:Y-m-d', 'after:start_date'],
]);
if (!$validator->validate()) {
print_r($validator->getErrors());
}
Define custom error messages for specific rules:
use Mousav1\Validify\Validator;
$data = [
'username' => '',
'email' => 'invalid-email',
];
$validator = new Validator($data, [
'username' => ['required'],
'email' => ['required', 'email'],
]);
$validator->setCustomMessages([
'username.required' => 'The username field cannot be empty.',
'email.email' => 'Please provide a valid email address.',
]);
if (!$validator->validate()) {
print_r($validator->getErrors());
}
Use aliases for more readable error messages:
$validator->setAliases([
'email' => 'Email Address'
]);
$validator->validate();
print_r($validator->getErrors());
Execute logic before validation starts:
$validator->beforeValidate(function (&$data) {
$data['username'] = strtolower($data['username']);
});
- required
- min
- max
- numeric
- confirmed
- url
- in
- between
- regex
- alpha
- optional
- required_with
- array
- integer
- boolean
- not_in
- uppercase
- lowercase
- json
- date_format
- after
- before
Contributions are welcome! Feel free to submit a pull request or open an issue.
- Fork the repository.
- Create a new branch for your feature/bugfix.
- Write clear and descriptive commit messages.
- Submit a pull request with detailed explanation.
This package is licensed under the MIT License. See the LICENSE file for more details.