Skip to content

PHPValidator is a modern PHP library for data validation in your PHP applications. It provides a flexible and extensible way to validate data using predefined rules or by creating custom validation rules.

License

v1p3r75/PHPValidator

 
 

Repository files navigation

PHPValidator

PHPValidator is a modern PHP library for data validation in your PHP applications. It provides a flexible and extensible way to validate data using predefined rules or by creating custom validation rules.

Packagist Version (custom server) Packagist Version (custom server) Packagist Version (custom server)

Installation

Use Composer to install PHPValidator:

composer require blakvghost/php-validator

Usage

use BlakvGhost\PHPValidator\Validator;
use BlakvGhost\PHPValidator\ValidatorException;

try {

    $data = [
        'username' => 'BlakvGhost',
        'email' => 'example@example.com',
        'score' => 42,
    ];
    // or
    // $data = $_POST;

    $validator = new Validator($data, [
        'username' => 'required|string',
        'email' => 'required|email',
        'score' => ['required','max_length:200', new CustomRule()],
        'password' => new CustomRule(),
    ]);

    if ($validator->isValid()) {
        echo "Validation passed!";
    } else {
        $errors = $validator->getErrors();
        print_r($errors);
    }
} catch (ValidatorException $e) {
    echo "Validation error: " . $e->getMessage();
}

Features

  • Predefined Rules: PHPValidator comes with a set of predefined validation rules such as required, string, email, maxLength etc.

  • Custom Rules: Easily create custom validation rules by implementing the RuleInterface.

  • Multilingual Support: Customize validation error messages based on the application's language using the LangManager.

List of Predefined Rules

PHPValidator provides a variety of predefined rules that you can use for data validation. Here is a list of some commonly used rules along with examples of their usage:

  1. Required Rule

    • Ensures that a field is present in the data.
    'username' => 'required'
  2. String Rule

    • Checks if a field is of string type.
    'username' => 'string'
  3. Email Rule

    • Validates that a field is a well-formed email address.
    'email' => 'email'
  4. Max Length Rule

    • Specifies the maximum length of a string field.
    'username' => 'max_length:25'
  5. Confirmed Rule

    • Checks if a field's value is the same as another field (commonly used for password confirmation).
    'password_confirmation' => 'confirmed:password'
  6. File Rule

    • Validates that a field is a file upload.
    'file' => 'file'
  7. Accepted Rule

    • Validates that a field is "yes", "on", "1", or true. Useful for checkboxes.
    'terms_and_conditions' => 'accepted'
  8. Accepted If Rule

    • Validates that a field is accepted if another field is equal to a specified value.
    'terms_and_conditions' => 'accepted_if:is_adult,true'
  9. ActiveURL Rule

    • Validates that a field is a valid, active URL.
    'website' => 'active_url'
  10. Alpha Rule

    • Validates that a field contains only alphabetic characters.
    'name' => 'alpha'
  11. Numeric Rule

    • Validates that a field contains only numeric characters.
    'age' => 'numeric'
  12. Lowercase Rule

    • Validates that a field contains only lowercase alphabetic characters.
    'username' => 'lower'
  13. Uppercase Rule

    • Validates that a field contains only uppercase alphabetic characters.
    'username' => 'upper'
  14. In Rule

    • Validates that a field's value is among a list of predefined values.
    'role' => 'in:admin,editor,viewer'
  15. Nullable Rule

    • Allows a field to be null or empty.
    'optional_field' => 'nullable'
  16. Password Rule

    • Validates that a field is a secure password.
    'password' => 'password'
  17. Same Rule

    • Validates that a field's value is the same as the value of another field.
    'password_confirmation' => 'same:password'
  18. Max Length Rule

    • Specifies the minimum length of a string field.
    'username' => 'min_length:8'
  19. Not In Rule

    • Validates that a field's value is not in a specified set.
    'value' => 'not_in:["foo", "bar"]'
  20. Required With Rule

    • Requires the field to be present if another specified field is present.
    'firstname' => 'required_with:lastname',
  21. Valid IP Rule

    • Validates that a field's value is a valid IP address.
    'client_ip' => 'valid_ip',
  22. Json Rule

    • Validates that a field's value is a valid JSON string.
    'config' => 'json',
  23. URL Rule

    • Validates that a field's value is a valid URL.
    'website' => 'url',
  24. Alpha Numeric Rule

    • Validates that a field's value contains only alphanumeric characters.
    'pseudo' => 'alpha_numeric',
  25. Boolean Rule

    • Validates that a field's value is a boolean.
    'is_admin' => 'boolean',
  26. Size Rule

    • Validates that the size of a string, integer, array, or file is equal to a specified value.
        [
            'string' =>'size:7', // strlen(string) == 7
            'integer' =>'size:7', // integer == 7
            'array' =>'size:7', // count(array) == 7
            'file' =>'size:512', // file size (kb) == 512
        ]

Custom Rule

In addition to the predefined rules, you can create custom validation rules by implementing the RuleInterface. Here's an example of how to create and use a custom rule:

CustomPasswordRule.php

// CustomPasswordRule.php
namespace YourNameSpace\Rules;

use BlakvGhost\PHPValidator\Rules\RuleInterface;
use BlakvGhost\PHPValidator\LangManager;

class CustomPasswordRule implements RuleInterface
{
    protected $field;

    public function __construct(protected array $parameters = [])
    {
    }

    public function passes(string $field, $value, array $data): bool
    {
        $this->field = $field;
        // Implement your custom validation logic here
        // Example: Check if the password is equal to confirm_password
        return $value === $data['confirm_password'];
    }

    public function message(): string
    {
        return "Vos deux mot de passes ne sont pas identiques";
    }
}

Usage in Validator

use BlakvGhost\PHPValidator\Validator;
use BlakvGhost\PHPValidator\ValidatorException;
use YourNameSpace\CustomPasswordRule;


// ...

try {

    $data = [
        'password' => '42',
        'confirm_password' => '142',
    ];
    // or
    // $data = $_POST;

    $validator = new Validator($data, [
        'password' => ['required', new CustomPasswordRule()],
    ]);

    if ($validator->isValid()) {
        echo "Validation passed!";
    } else {
        $errors = $validator->getErrors();
        print_r($errors);
    }
} catch (ValidatorException $e) {
    echo "Validation error: " . $e->getMessage();
}

In this example, we created a CustomPasswordRule that checks if the password is equal to confirm_password. You can customize the passes method to implement your specific validation logic.

Contributing

If you would like to contribute to PHPValidator, please follow our Contribution Guidelines.

Authors

Support

For support, you can reach out to me by email at dev@kabirou-alassane.com. Feel free to contact me if you have any questions or need assistance with PHPValidator.

License

PHPValidator is open-source software licensed under the MIT license.

About

PHPValidator is a modern PHP library for data validation in your PHP applications. It provides a flexible and extensible way to validate data using predefined rules or by creating custom validation rules.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%