axm/validation

Simple, validation of rules

This package's canonical repository appears to be gone and the package has been frozen as a result.

1.0.9 2024-01-22 12:39 UTC

This package is auto-updated.

Last update: 2024-02-22 12:54:36 UTC


README

Total Downloads Latest Stable Version License

Certainly! Here's comprehensive documentation for the Validator class along with examples of how to use it. This documentation is formatted for GitHub:

Validator Class

The Validator class is a versatile tool for data validation in PHP applications. It allows you to define validation rules and check if data meets those rules. This documentation provides an in-depth guide to using the Validator class.

Table of Contents

  1. Class Overview
  2. Installation
  3. Basic Usage
  4. Validation Rules
  5. Advanced Usage
  6. Examples
  7. Contributing
  8. License

1. Class Overview

The Validator class provides the following features:

  • Validation of data against user-defined rules.
  • Support for custom validation rules.
  • Rule chaining for complex validation scenarios.
  • Detailed error reporting with customizable error messages.

2. Installation

To use the Validator class, follow these steps:

composer require axm/validation

3. Basic Usage

Here's a simple example of how to use the Validator class:

use Axm\Validation\Validator;

$rules = [
    'username' => 'required|string|min:5|max:20',
    'email' => 'required|email',
];

$data = [
    'username' => 'john_doe',
    'email' => 'johndoe@example.com',
];

$validator = Validator::make($rules, $data);

if ($validator->validate()) {
    echo "Data is valid!";
} else {
    $errors = $validator->getErrors();
    print_r($errors);
}

4. Validation Rules

The Validator class supports a variety of validation rules, including required, string, email, min, max, and custom rules. You can chain rules together using the | separator.

Available Rules
  • required: Ensures the field is present and not empty.
  • string: Checks if the field is a string.
  • email: Validates the field as an email address.
  • min:value: Checks if the field's length or value is greater than or equal to value.
  • max:value: Checks if the field's length or value is less than or equal to value.
  • Custom Rules: You can define your own custom validation rules.

5. Advanced Usage

Custom Validation Rules

You can create custom validation rules by defining a class that implements the Axm\Validation\Rules\RuleInterface. Here's an example:

use Axm\Validation\Rules\RuleInterface;

class CustomRule implements RuleInterface
{
    public function validate($value, array $parameters = []): bool
    {
        // Implement your custom validation logic here.
        return /* validation result */;
    }

    public function message(): string
    {
        // Define a custom error message for this rule.
        return 'Custom validation failed.';
    }
}
Adding Custom Rules

To add custom rules to the Validator, use the addRules method:

$customRules = [
    'custom_rule' => CustomRule::class,
];

$validator->addRules($customRules);

6. Examples

Here are more examples of how to use the Validator class:

Example 1: Conditional Validation
$rules = [
    'password' => 'required|string|min:8',
    'confirm_password' => 'required|string|same:password',
];

$data = [
    'password' => 'mysecurepassword',
    'confirm_password' => 'mysecurepassword',
];

$validator = Validator::make($rules, $data);
Example 2: Unique Rule
$rules = [
    'email' => 'required|string|unique:users,email',
];

$data = [
    'email' => 'johndoe@example.com',
];

$validator = Validator::make($rules, $data);

7. Contributing

If you want to contribute to the Validator class, please follow the contributing guidelines.

8. License

The Validator class is open-source software licensed under the MIT License.

This documentation should help you get started with the Validator class and demonstrate various use cases. For more details and advanced usage, refer to the class source code and examples provided in the GitHub repository.

Feel free to adapt and expand upon this documentation as needed for your project.