Xentixar Validator is a PHP validation library that provides a robust mechanism for validating data based on various rules. It includes a command-line tool for publishing configuration files and a Validator
class for data validation.
- Data validation with rules like required, email, min, max, unique, and more.
- Customizable error messages.
- Integration with a PDO-based database for unique and existence checks.
- Command-line utility for managing configuration files.
composer require xentixar/validator
Before using the validator, you'll need to set up configuration files.
Run the following command to publish the default configuration files to your project:
cd vendor/xentixar/validator/bin
php xentixar publish:config
This command will copy the configuration files to config/vendor/validator
:
database.php
for database connection settings.messages.php
for custom validation messages.
Provide your database connection settings here. Example:
<?php
return [
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => '3306',
'database' => 'your_database',
'username' => 'your_username',
'password' => 'your_password',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
];
Define custom error messages for validation rules. Example:
<?php
return [
'required' => 'The :field field is required.',
'email' => 'The :field field must be a valid email address.',
'min' => 'The :field field must be at least :param characters.',
'max' => 'The :field field must not exceed :param characters.',
'between' => 'The :field field must be between :min and :max characters.',
'numeric' => 'The :field field must be a number.',
'integer' => 'The :field field must be an integer.',
'url' => 'The :field field must be a valid URL.',
'date' => 'The :field field must be a valid date (Y-m-d).',
'confirmed' => 'The :field confirmation does not match.',
'same' => 'The :field field must match the :param field.',
'unique' => 'The :field field must be unique.',
'exists' => 'The :field field does not exist.',
'in' => 'The :field field must be one of the following values: :values.',
'regex' => 'The :field field format is invalid.',
'size' => 'The :field field must be exactly :param characters.',
'date_format' => 'The :field field does not match the format :param.',
'file' => 'The :field field must be a valid uploaded file.',
'mimes' => 'The :field field must be a file of type: :types.',
];
Here's a quick example of how to use the Validator
class:
use Xentixar\Validator\Validator;
$validator = new Validator();
$data = [
'email' => '[email protected]',
'password' => 'password123',
];
$rules = [
'email' => 'required|email',
'password' => 'required|min:6',
];
if ($validator->validate($data, $rules)) {
echo "Validation passed!";
} else {
print_r($validator->errors());
}
You can customize error messages in the messages.php
configuration file. The validator will use these messages if a rule fails.
publish:config
: Copies the default configuration files to theconfig/vendor/validator
directory.
To run a command, use:
php xentixar [command]
For example:
php xentixar publish:config
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/YourFeature
). - Commit your changes (
git commit -am 'Add new feature'
). - Push to the branch (
git push origin feature/YourFeature
). - Create a new Pull Request.
This package is licensed under the MIT License.
For questions or support, please open an issue on GitHub.