PHPackages                             olegkishko/php-field-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. olegkishko/php-field-validator

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

olegkishko/php-field-validator
==============================

Lightweight, extensible validation library with chainable rules.

v1.0.0(1y ago)01MITPHPPHP ^8.3

Since May 18Pushed 1y ago1 watchersCompare

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

READMEChangelogDependencies (3)Versions (2)Used By (0)

PHP Field Validator
===================

[](#php-field-validator)

A flexible and extensible PHP validation library that allows you to create chain validation rules for your data fields.

Features
--------

[](#features)

- Chain multiple validation rules
- Built-in validation rules
- Custom validation rules support
- Callback-based validation
- Easy to extend and implement

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

[](#installation)

```
composer require olegkishko/php-field-validator
```

Basic Usage
-----------

[](#basic-usage)

```
use OlegKishko\PhpFieldValidator\Validator;

$validator = new Validator();
$validator->field('first_name')->required()->maxLength(250);
$validator->field('last_name')->required()->maxLength(250);
$validator->field('email')->required()->email()->maxLength(250);
$validator->field('age')->integer()->between(14, 100);
$validator->field('gender')->in('M', 'F');
$validator->field('linkedin')->url()->maxLength(250);

$input = [
    'first_name' => 'John',
    'last_name' => 'Doe',
    'email' => 'john.doe@example.com',
    'age' => 35,
    'gender' => 'M',
    'linkedin' => '',
];

$validator->validate($input);

if ($validator->hasErrors()) {
    var_dump($validator->getErrors());
}
```

Built-in Rules
--------------

[](#built-in-rules)

- `array()` - Validates if value is an array
- `between(int $min, int $max)` - Validates if numeric value is between specified range
- `boolean()` - Validates if value is a boolean
- `contains(string $needle)` - Validates if value contains a substring
- `count(int $count)` - Validates if value is an array and consists of the specified number of elements
- `distinct()` - Validates if value is an array and all elements are unique
- `each(array $rules)` - Validates if value is an array and each element is valid, according to the rules
- `email()` - Validates email format
- `endsWith(string $suffix)` - Validates if value ends with a suffix
- `equals(string|int|float|array $to)` - Validates if value equals to passed value
- `false()` - Validates if value is a boolean FALSE
- `float()` - Validates if value is a float
- `in(array $choices)` - Validates if value is in passed choices
- `integer()` - Validates if value is an integer
- `ip(string $version)` - Validates if value is an IP (any, ipv4 only, ipv6 only)
- `length(int $length)` - Validates if value is a string with the specified length
- `maxCount(int $maxCount)` - Validates if value is an array and contains no more the specified number of elements
- `maxLength(int $length)` - Validates if value is a string and its length is not more than the specified length
- `max(int|float $max)` - Validates if value is a numeric and not more than the specified value
- `minCount(int $minCount)` - Validates if value is an array and contains at least the specified number of elements
- `minLength(int $length)` - Validates if value is a string and its length is at least the specified length
- `min(int|float $min)` - Validates if value is a numeric and at least the specified value
- `integer()` - Validates if value is a negative numeric
- `notBlank()` - Validates if value is not empty
- `notEquals(string|int|float|array $to)` - Validates if value not equals to passed value
- `notIn(array $choices)` - Validates if value is not in passed choices
- `numeric()` - Validates if value is numeric
- `positive()` - Validates if value is a positive numeric
- `regex(string $pattern)` - Validates value against a custom regex pattern
- `required()` - Validates if value is not empty
- `startsWith(string $prefix)` - Validates if value starts with a prefix
- `string()` - Validates if value is a string
- `true()` - Validates if value is a boolean TRUE
- `url()` - Validates URL format

Chaining Rules
--------------

[](#chaining-rules)

You can chain multiple rules for complex validation scenarios:

```
$validator
    ->field('first_name')
    ->required()
    ->minLength(3)
    ->maxLength(20);
```

Callback Rules
--------------

[](#callback-rules)

Use callback rules for custom validation logic:

```
$validator
    ->field('password')
    ->required()
    ->callback(
        function (string $value): bool {
            return preg_match('/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/', $value);
        },
        'Password must contain at least one letter and one number',
    );
```

Custom Rules
------------

[](#custom-rules)

You can create custom validation rules by implementing the `RuleInterface`:

```
use OlegKishko\PhpFieldValidator\Rules\RuleInterface;

class CustomPasswordRule implements RuleInterface
{
    public function validate(mixed $value): bool
    {
        // Your custom validation logic
        return preg_match('/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/', $value);
    }

    public function getErrorMessage(): string
    {
        return 'Password must contain at least one letter, one number, and one special character';
    }
}

// Usage
$validator
    ->field('password')
    ->customRule(new CustomPasswordRule());

$validator->validate(['password' => 'simplepassword!']);
```

Error Messages
--------------

[](#error-messages)

When validation hasErrors, you can retrieve error messages:

```
$validator->validate($input);

if ($validator->hasErrors()) {
    foreach ($validator->getErrors() as $field => $error) {
        echo sprintf('[%s]: %s%s', $field, $error, PHP_EOL);
    }
}
```

License
-------

[](#license)

This project is licensed under the MIT License — see the LICENSE file for details.

Contributing
------------

[](#contributing)

Contributions are welcome! Please feel free to submit a Pull Request.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance50

Moderate activity, may be stable

Popularity1

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

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

Unknown

Total

1

Last Release

365d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/999a0eb790d7445737a5a06308ad8259399aaf1e4c852db4dd3f1f4224a155f2?d=identicon)[olegkishko](/maintainers/olegkishko)

---

Top Contributors

[![olegkishko](https://avatars.githubusercontent.com/u/23615391?v=4)](https://github.com/olegkishko "olegkishko (7 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

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