PHP Validator is a lightweight, modular validation library designed to help you validate your data effortlessly. Whether you're handling simple form submissions or complex data structures, this library provides a flexible and extensible framework to ensure your data is clean, safe, and ready for use.
- Easy to Use: Define validation rules with minimal effort, and apply them across your data with a single method call.
- Modular Design: Choose and use only the validators you need, making it easy to extend and customize the validation process.
- Extensible: Easily create custom validation rules to handle your specific data requirements.
- Comprehensive Error Handling: Automatically capture and handle validation errors, allowing you to provide clear feedback to users.
- Support for Complex Data Structures: Validate arrays and nested data with specialized validators like
ArrayValidator
.
Install the library via Composer:
composer require openvoid/php-validator
The PHP Validator library is designed to be extremely user-friendly and modular, allowing you to validate data in a way that suits your application's needs. Whether you're validating simple data fields or complex nested arrays, PHP Validator provides an intuitive API that makes data validation straightforward.
In this example, we demonstrate how to set up a simple validator for basic data fields such as age
, website_ids
, and email
. Each field has its own set of validation rules, ensuring that the data meets your application's requirements.
<?php
require __DIR__ . '/vendor/autoload.php';
use OpenVoid\Validator\Validator;
use OpenVoid\Validator\Validators\ArrayValidator;
use OpenVoid\Validator\Validators\EmailValidator;
use OpenVoid\Validator\Validators\Integer;
use OpenVoid\Validator\Validators\NotNull;
$validator = new Validator([
"age" => [(new Integer())->min(18)->max(99)],
"website_ids" => [(new ArrayValidator([new Integer(), new NotNull()]))->not_empty()],
"email" => new EmailValidator(),
]);
$is_validated = $validator->validate([
"age" => 24,
"website_ids" => [1, 2],
"email" => "antonio@obradovic.dev",
]);
if ($is_validated) {
// * Process form
} else {
// * Handle errors
$errors = $validator->get_errors();
}
In a real-world scenario, you often need to validate data submitted through forms. This example shows how to validate POST data by defining validation rules for each form field. Simply pass the entire $_POST
object into the validate method and let the library handle the rest.
<?php
require __DIR__ . '/vendor/autoload.php';
use OpenVoid\Validator\Validator;
use OpenVoid\Validator\Validators\EmailValidator;
use OpenVoid\Validator\Validators\NotNull;
$validator = new Validator([
"email" => [new EmailValidator(), new NotNull()],
]);
$is_validated = $validator->validate($_POST);
if ($is_validated) {
// * Process form
} else {
// * Handle errors
$errors = $validator->get_errors();
}
This project is licensed under the MIT License - see the LICENSE file for details.