olegkishko/php-field-validator

Lightweight, extensible validation library with chainable rules.

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/olegkishko/php-field-validator

v1.0.0 2025-05-18 14:08 UTC

This package is auto-updated.

Last update: 2025-12-18 15:35:28 UTC


README

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

Features

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

Installation

composer require olegkishko/php-field-validator

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

  • 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

You can chain multiple rules for complex validation scenarios:

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

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

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

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

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

Contributing

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