PHPackages                             phpdevcommunity/php-validator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. phpdevcommunity/php-validator

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

phpdevcommunity/php-validator
=============================

PHP Validator is a fast, extensible, and simple PHP validation library that allows you to easily validate various types of data.

1.0.2(8mo ago)254[1 issues](https://github.com/phpdevcommunity/php-validator/issues)MITPHPPHP &gt;=7.4

Since Oct 8Pushed 8mo ago2 watchersCompare

[ Source](https://github.com/phpdevcommunity/php-validator)[ Packagist](https://packagist.org/packages/phpdevcommunity/php-validator)[ RSS](/packages/phpdevcommunity-php-validator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (4)Used By (0)

PHP Validator
=============

[](#php-validator)

PHP Validator is a fast, extensible, and simple PHP validation library that allows you to easily validate various types of data.

Installation
------------

[](#installation)

You can install this library via [Composer](https://getcomposer.org/). Ensure your project meets the minimum PHP version requirement of 7.4.

```
composer require phpdevcommunity/php-validator
```

Requirements
------------

[](#requirements)

- PHP version 7.4 or higher
- Required package for PSR-7 HTTP Message (e.g., `guzzlehttp/psr7`)

Usage
-----

[](#usage)

The PHP Validator library enables you to validate data in a simple and flexible manner using pre-configured validation rules. Here are some usage examples:

### Example 1: Email Address Validation

[](#example-1-email-address-validation)

Validate an email address to ensure it is not null and matches the standard email format.

```
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Email;

// Instantiate Validation object for email validation
$validation = new Validation([
    'email' => [new NotNull(), new Email()]
]);

// Example data array
$data = ['email' => 'john.doe@example.com'];

// Validate the data
if ($validation->validateArray($data) === true) {
    echo "Email is valid!";
} else {
    $errors = $validation->getErrors();
    echo "Validation errors: " . implode(", ", $errors['email']);
}
```

### Example 2: Age Validation

[](#example-2-age-validation)

Validate the age to ensure it is a non-null integer and is 18 or older.

```
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Integer;

// Instantiate Validation object for age validation
$validation = new Validation([
    'age' => [new NotNull(), new Integer(['min' => 18])]
]);

// Example data array
$data = ['age' => 25];

// Validate the data
if ($validation->validateArray($data) === true) {
    echo "Age is valid!";
} else {
    $errors = $validation->getErrors();
    echo "Validation errors: " . implode(", ", $errors['age']);
}
```

### Example 3: Converting Empty Strings to `null`

[](#example-3-converting-empty-strings-to-null)

By default, when using `validate(ServerRequestInterface $request)`, the **convertEmptyToNull** option is automatically enabled. This ensures that all empty strings (`""`) are converted to `null` before validation.

When using `validateArray(array $data)` directly, you need to enable this option manually:

```
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Item;
use PhpDevCommunity\Validator\Assert\Alphabetic;

// Define validation rules
$validation = new Validation([
    'person' => [new NotNull(), new Item([
        'first_name' => [new NotNull(), new Alphabetic()],
        'last_name'  => [new NotNull(), new Alphabetic()],
    ])]
]);

// Example input with an empty string
$validInput = [
    'person' => [
        'first_name' => '',     // will be converted to null
        'last_name'  => 'Doe'
    ]
];

// Manually enable conversion of "" to null
$validation->convertEmptyToNull();

if ($validation->validateArray($validInput) === true) {
    echo "Data is valid!";
} else {
    $errors = $validation->getErrors();
    echo "Validation errors: " . json_encode($errors, JSON_PRETTY_PRINT);
}
```

In this example:

- Before validation, empty strings are converted to `null`.
- This prevents validators such as `NotNull()` from failing because of an empty string.
- When using `validate($request)` with a PSR-7 request, this option is automatically enabled.

### Additional Examples

[](#additional-examples)

Let's explore more examples covering various validators:

#### Username Validation

[](#username-validation)

Ensure that a username is not null, has a minimum length of 3 characters, and contains only alphanumeric characters.

```
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Alphanumeric;
use PhpDevCommunity\Validator\Assert\StringLength;

// Instantiate Validation object for username validation
$validation = new Validation([
    'username' => [new NotNull(), new StringLength(['min' => 3]), new Alphanumeric()]
]);

// Example data array
$data = ['username' => 'john_doe123'];

// Validate the data
if ($validation->validateArray($data) === true) {
    echo "Username is valid!";
} else {
    $errors = $validation->getErrors();
    echo "Validation errors: " . implode(", ", $errors['username']);
}
```

#### Example: Validating Nested Objects with `Item`

[](#example-validating-nested-objects-with-item)

The `Item` rule allows you to validate nested objects or associative arrays with specific rules for each key.

```
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Item;
use PhpDevCommunity\Validator\Assert\StringLength;
use PhpDevCommunity\Validator\Assert\Alphabetic;

// Define validation rules for a nested object (e.g., a "person" object)
$validation = new Validation([
    'person' => [new NotNull(), new Item([
        'first_name' => [new NotNull(), new Alphabetic(), (new StringLength())->min(3)],
        'last_name' => [new NotNull(), new Alphabetic(), (new StringLength())->min(3)],
    ])]
]);

// Example data
$data = [
    'person' => [
        'first_name' => 'John',
        'last_name' => 'Doe'
    ]
];

// Validate the data
if ($validation->validateArray($data) === true) {
    echo "Person object is valid!";
} else {
    $errors = $validation->getErrors();
    echo "Validation errors: " . json_encode($errors, JSON_PRETTY_PRINT);
}
```

#### Example: Validating Arrays of Items with `Collection`

[](#example-validating-arrays-of-items-with-collection)

The `Collection` rule is used to validate arrays where each item in the array must satisfy a set of rules.

```
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotEmpty;
use PhpDevCommunity\Validator\Assert\Collection;
use PhpDevCommunity\Validator\Assert\Item;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\StringLength;

// Define validation rules for a collection of articles
$validation = new Validation([
    'articles' => [new NotEmpty(), new Collection([
        new Item([
            'title' => [new NotNull(), (new StringLength())->min(3)],
            'body' => [new NotNull(), (new StringLength())->min(10)],
        ])
    ])]
]);

// Example data
$data = [
    'articles' => [
        ['title' => 'Article 1', 'body' => 'This is the body of the first article.'],
        ['title' => 'Article 2', 'body' => 'Second article body here.']
    ]
];

// Validate the data
if ($validation->validateArray($data) === true) {
    echo "Articles are valid!";
} else {
    $errors = $validation->getErrors();
    echo "Validation errors: " . json_encode($errors, JSON_PRETTY_PRINT);
}
```

#### URL Validation

[](#url-validation)

Validate a URL to ensure it is not null and is a valid URL format.

```
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Url;

// Instantiate Validation object for URL validation
$validation = new Validation([
    'website' => [new NotNull(), new Url()]
]);

// Example data array
$data = ['website' => 'https://example.com'];

// Validate the data
if ($validation->validateArray($data) === true) {
    echo "Website URL is valid!";
} else {
    $errors = $validation->getErrors();
    echo "Validation errors: " . implode(", ", $errors['website']);
}
```

#### Numeric Value Validation

[](#numeric-value-validation)

Validate a numeric value to ensure it is not null and represents a valid numeric value.

```
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Numeric;

// Instantiate Validation object for numeric value validation
$validation = new Validation([
    'price' => [new NotNull(), new Numeric()]
]);

// Example data array
$data = ['price' => 99.99];

// Validate the data
if ($validation->validateArray($data) === true) {
    echo "Price is valid!";
} else {
    $errors = $validation->getErrors();
    echo "Validation errors: " . implode(", ", $errors['price']);
}
```

#### Custom Validation Rule

[](#custom-validation-rule)

Implement a custom validation rule using a callback function.

```
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Custom;

// Custom validation function to check if the value is a boolean
$isBoolean = function ($value) {
    return is_bool($value);
};

// Instantiate Validation object with a custom validation rule
$validation = new Validation([
    'active' => [new NotNull(), new Custom($isBoolean)]
]);

// Example data array
$data = ['active' => true];

// Validate the data
if ($validation->validateArray($data) === true) {
    echo "Value is valid!";
} else {
    $errors = $validation->getErrors();
    echo "Validation errors: " . implode(", ", $errors['active']);
}
```

Certainly! Below is a chapter you can add to your README specifically covering examples for using the `validate(ServerRequestInterface $request)` method from your `Validation` class.

Using `validate(ServerRequestInterface $request)` Method
--------------------------------------------------------

[](#using-validateserverrequestinterface-request-method)

The `Validation` class provides a convenient method `validate(ServerRequestInterface $request)` to validate data extracted from a `\Psr\Http\Message\ServerRequestInterface` object. This method simplifies the process of validating request data typically received in a web application.

### Example 1: Validating User Registration Form

[](#example-1-validating-user-registration-form)

Suppose you have a user registration form with fields like `username`, `email`, `password`, and `age`. Here's how you can use the `validate` method to validate this form data:

```
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Email;
use PhpDevCommunity\Validator\Assert\Integer;

// Define validation rules for each field
$validation = new Validation([
    'username' => [new NotNull()],
    'email' => [new NotNull(), new Email()],
    'password' => [new NotNull()],
    'age' => [new NotNull(), new Integer()]
]);

// Assume $request is the \Psr\Http\Message\ServerRequestInterface object containing form data
if ($validation->validate($request) === true) {
    // Validation passed, retrieve validated data
    $validatedData = $validation->getData();
    // Process registration logic here (e.g., save to database)
    // $username = $validatedData['username'];
    // $email = $validatedData['email'];
    // $password = $validatedData['password'];
    // $age = $validatedData['age'];
    echo "Registration successful!";
} else {
    // Validation failed, retrieve validation errors
    $errors = $validation->getErrors();
    // Handle validation errors (e.g., display error messages to the user)
    echo "Validation errors: " . implode(", ", $errors);
}
```

In this example:

- We instantiate a `Validation` object with validation rules defined for each field (`username`, `email`, `password`, `age`).
- We call the `validate` method with the `$request` object containing form data.
- If validation passes (`validate` method returns `true`), we retrieve the validated data using `$validation->getData()` and proceed with the registration logic.
- If validation fails, we retrieve the validation errors using `$validation->getErrors()` and handle them accordingly.

### Example 2: Validating API Input Data

[](#example-2-validating-api-input-data)

Consider validating input data received via an API endpoint. Here's how you can use the `validate` method in this context:

```
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Numeric;

// Define validation rules for API input data
$validation = new Validation([
    'product_id' => [new NotNull(), new Numeric()],
    'quantity' => [new NotNull(), new Numeric()]
]);

// Assume $request is the \Psr\Http\Message\ServerRequestInterface object containing API input data
if ($validation->validate($request) === true) {
    // Validation passed, proceed with processing API request
    $validatedData = $validation->getData();
    // Extract validated data
    $productId = $validatedData['product_id'];
    $quantity = $validatedData['quantity'];
    echo "API request validated successfully!";
} else {
    // Validation failed, retrieve validation errors
    $errors = $validation->getErrors();
    // Handle validation errors (e.g., return error response to the client)
    echo "Validation errors: " . implode(", ", $errors);
}
```

In this example:

- We define validation rules for `product_id` and `quantity`.
- We call the `validate` method with the `$request` object containing API input data.
- If validation passes, we retrieve the validated data using `$validation->getData()` and proceed with processing the API request.
- If validation fails, we retrieve the validation errors using `$validation->getErrors()` and handle them appropriately.

---

### Additional Features

[](#additional-features)

- **Simple Interface**: Easily define validation rules using a straightforward interface.
- **Extensible**: Extend the library with custom validation rules by implementing the `ValidatorInterface`.
- **Error Handling**: Retrieve detailed validation errors for each field.

---

License
-------

[](#license)

This library is open-source software licensed under the [MIT license](LICENSE).

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance51

Moderate activity, may be stable

Popularity12

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~172 days

Total

3

Last Release

244d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/909a078010ad44ff35146af8288451a3b6fd26f81cb198cbea776a92553c9b8a?d=identicon)[F.Michel](/maintainers/F.Michel)

---

Top Contributors

[![michelphp](https://avatars.githubusercontent.com/u/26349908?v=4)](https://github.com/michelphp "michelphp (5 commits)")

---

Tags

php-libraryphp-validationphp-validatorvalidation-libraryvalidator

### Embed Badge

![Health badge](/badges/phpdevcommunity-php-validator/health.svg)

```
[![Health](https://phpackages.com/badges/phpdevcommunity-php-validator/health.svg)](https://phpackages.com/packages/phpdevcommunity-php-validator)
```

###  Alternatives

[aporat/store-receipt-validator

PHP receipt validator for Apple App Store and Amazon Appstore

6503.9M9](/packages/aporat-store-receipt-validator)[laminas/laminas-validator

Validation classes for a wide range of domains, and the ability to chain validators to create complex validation criteria

15644.9M188](/packages/laminas-laminas-validator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
