PHPackages                             programmatordev/fluent-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. programmatordev/fluent-validator

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

programmatordev/fluent-validator
================================

A Symfony Validator wrapper that enables fluent-style validation for raw values, offering an easy-to-use and intuitive API to validate user input or other data in a concise and readable manner.

v1.0.0(5mo ago)22MITPHPPHP &gt;=8.4CI passing

Since Mar 13Pushed 5mo agoCompare

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

READMEChangelog (4)Dependencies (7)Versions (6)Used By (0)

Fluent Validator
================

[](#fluent-validator)

[![Latest Version](https://camo.githubusercontent.com/d9e58909e7bb80276eaba9a77ca5679ac6a6b0d2b5a62d16223778a2c35c6f61/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f70726f6772616d6d61746f726465762f666c75656e742d76616c696461746f722e7376673f7374796c653d666c61742d737175617265)](https://github.com/programmatordev/fluent-validator/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Tests](https://github.com/programmatordev/fluent-validator/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/programmatordev/fluent-validator/actions/workflows/ci.yml?query=branch%3Amain)

A [Symfony Validator](https://symfony.com/doc/current/validation.html) wrapper that enables fluent-style validation for raw values, offering an easy-to-use and intuitive API to validate user input or other data in a concise and readable manner.

Note

This library will always (try to) be in sync with the latest Symfony Validator version.

Features
--------

[](#features)

- 🌊 **Fluent-style validation:** Chain validation methods for better readability and flow.
- 🤘 **Constraints autocompletion:** Enables IDE autocompletion for available constraints.
- 🔥 **Three validation methods:** Use `validate`, `assert`, or `isValid` based on the context (i.e., collect errors or throw exceptions).
- ⚙️ **Custom constraints:** Integrate custom validation logic with Symfony's Validator system.
- 💬 **Translations support:** Translate validation error messages into multiple languages.

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Usage](#usage)
- [Constraints](#constraints)
- [Methods](#methods)
    - [validate](#validate)
    - [assert](#assert)
    - [isValid](#isvalid)
    - [toArray](#toarray)
    - [addNamespace](#addnamespace)
    - [setTranslator](#settranslator)
- [Custom Constraints](#custom-constraints)
- [Translations](#translations)

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

[](#requirements)

- PHP 8.4 or higher.

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

[](#installation)

Install via [Composer](https://getcomposer.org/):

```
composer require programmatordev/fluent-validator
```

Usage
-----

[](#usage)

Simple usage example:

```
use ProgrammatorDev\FluentValidator\Validator;

// example: validate the user's age to ensure it's between 18 and 60
$errors = Validator::notBlank()
    ->greaterThanOrEqual(18)
    ->lessThan(60)
    ->validate($age);

if ($errors->count() > 0) {
    // handle errors
}
```

Constraint autocompletion is available in IDEs like PhpStorm. The method names match Symfony constraints but with a lowercase first letter:

- `NotBlank` =&gt; `notBlank`
- `All` =&gt; `all`
- `PasswordStrength` =&gt; `passwordStrength`
- ...and so on.

For all available constraints, check the [Constraints](#constraints) section.

For all available methods, check the [Methods](#methods) section.

There is also a section for [Custom Constraints](#custom-constraints) and [Translations](#translations).

Constraints
-----------

[](#constraints)

All available constraints can be found on the [Symfony Validator documentation](https://symfony.com/doc/current/validation.html#constraints).

For custom constraints, check the [Custom Constraints](#custom-constraints) section.

Methods
-------

[](#methods)

### `validate`

[](#validate)

```
use Symfony\Component\Validator\Constraints\GroupSequence;

validate(mixed $value, ?string $name = null, string|GroupSequence|array|null $groups = null): ConstraintViolationListInterface
```

Returns a `ConstraintViolationList` object, acting as an array of errors.

```
use ProgrammatorDev\FluentValidator\Validator;

$errors = Validator::email()->validate('test@email.com');

if ($errors->count() > 0) {
    foreach ($errors as $error) {
        $message = $error->getMessage();
        // ...
    }
}
```

### `assert`

[](#assert)

```
use Symfony\Component\Validator\Constraints\GroupSequence;

assert(mixed $value, ?string $name = null, string|GroupSequence|array|null $groups = null): void
```

Throws a `ValidationFailedException` when validation fails.

```
use ProgrammatorDev\FluentValidator\Exception\ValidationFailedException;
use ProgrammatorDev\FluentValidator\Validator;

try {
    Validator::notBlank()->assert($name);
    Validator::notBlank()->email()->assert($email);
}
catch (ValidationFailedException $exception) {
    // the exception message will always be the first error thrown
    $message = $exception->getMessage();
    // value that failed validation
    $invalidValue = $exception->getInvalidValue();
    // get access to all errors
    // returns a ConstraintViolationList object like in the validate method
    $errors = $exception->getViolations();

    // ...
}
```

### `isValid`

[](#isvalid)

```
use Symfony\Component\Validator\Constraints\GroupSequence;

isValid(mixed $value, string|GroupSequence|array|null $groups = null): bool
```

Returns a `bool` indicating if the value is valid.

```
use ProgrammatorDev\FluentValidator\Validator;

if (!Validator::email()->isValid($email)) {
    // handle invalid email
}
```

### `toArray`

[](#toarray)

```
use Symfony\Component\Validator\Constraint;

/** @return Constraint[] */
toArray(): array
```

Returns an array with all added constraints.

```
use ProgrammatorDev\FluentValidator\Validator;

$constraints = Validator::notBlank()->email()->toArray();
```

It is useful for `Composite` constraints (i.e., a constraint that is composed of other constraints) and keeps the fluent-style validation:

```
use ProgrammatorDev\FluentValidator\Validator;

// validate that the array should have at least one value
// and each value should be between 0 and 100
$errors = Validator::count(min: 1)
    ->all(Validator::range(min: 0, max: 100)->toArray())
    ->validate($value);
```

### `addNamespace`

[](#addnamespace)

```
addNamespace(string $namespace): void
```

Used to add namespaces for custom constraints.

Check the [Custom Constraints](#custom-constraints) section.

### `setTranslator`

[](#settranslator)

```
use Symfony\Contracts\Translation\TranslatorInterface;

setTranslator(?TranslatorInterface $translator): void
```

Used to add a translator for validation error message translations.

Check the [Translations](#translations) section.

Custom Constraints
------------------

[](#custom-constraints)

If you need a custom constraint, follow the Symfony Validator documentation: [Creating Custom Constraints](https://symfony.com/doc/current/validation/custom_constraint.html).

### Example: Creating a `ContainsAlphanumeric` Constraint

[](#example-creating-a-containsalphanumeric-constraint)

#### 1. Create a Constraint Class

[](#1-create-a-constraint-class)

This class defines the error message and configurable options.

```
namespace App\Constraint;

use Symfony\Component\Validator\Constraint;

class ContainsAlphanumeric extends Constraint
{
    // set configurable options
}
```

#### 2. Create the Validator Class

[](#2-create-the-validator-class)

The validator checks if the value complies with the constraint rules.

```
namespace App\Constraint;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class ContainsAlphanumericValidator extends ConstraintValidator
{
    public function validate(mixed $value, Constraint $constraint): void
    {
        // custom validation logic
    }
}
```

#### 3. Register the Constraint Namespace

[](#3-register-the-constraint-namespace)

Register the namespace where the custom constraints will be located in your project.

```
use ProgrammatorDev\FluentValidator\Validator;

Validator::addNamespace('App\Constraint');

Validator::notBlank()->containsAlphanumeric()->isValid('!'); // false
Validator::notBlank()->containsAlphanumeric()->isValid('v4l1d'); // true
```

You can have multiple constraints in the same namespace or have multiple namespaces.

Note

Custom constraints will not be suggested in IDE autocompletion.

Translations
------------

[](#translations)

Set a global translator to handle error message translations.

```
use ProgrammatorDev\FluentValidator\Translator\Translator;

// set translator to Portuguese (Portugal) locale
Validator::setTranslator(new Translator('pt'));

// now all error messages will be in Portuguese
Validator::notBlank()->validate('');
```

To add your own translations, you can integrate a custom translator.

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

[](#contributing)

Any form of contribution to improve this library (including requests) will be welcome and appreciated. Make sure to open a pull request or issue.

License
-------

[](#license)

This project is licensed under the MIT license. Please see the [LICENSE](LICENSE) file distributed with this source code for further information regarding copyright and licensing.

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance72

Regular maintenance activity

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity59

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 ~67 days

Total

5

Last Release

161d ago

Major Versions

v0.3.0 → 1.x-dev2025-12-08

PHP version history (2 changes)v0.1.0PHP &gt;=8.2

1.x-devPHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/831bb375fc093d45e6f6ed3d7fb1d15656db5e0a8fca6ef3e212e60aa6e698b3?d=identicon)[programmatordev](/maintainers/programmatordev)

---

Top Contributors

[![andrepimpao](https://avatars.githubusercontent.com/u/41913452?v=4)](https://github.com/andrepimpao "andrepimpao (48 commits)")

---

Tags

fluent-validationphp-validationphp-validatorphp8symfony-validatorvalidationvalidatorvalidatorvalidationphp8php-validationsymfony-validatorphp-validatorfluent-validation

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/programmatordev-fluent-validator/health.svg)

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

###  Alternatives

[rollerworks/password-strength-validator

Password-strength validator for Symfony

1455.7M6](/packages/rollerworks-password-strength-validator)[barbieswimcrew/zip-code-validator

Constraint class for international zipcode validation

772.3M](/packages/barbieswimcrew-zip-code-validator)[iamfarhad/validation

🇮🇷 Complete Laravel Persian validation package - Iranian national ID, mobile numbers, Shamsi dates, IBAN/Sheba, postal codes &amp; more. Modern Laravel 10-12 support with both ValidationRule objects &amp; string-based rules.

2917.3k](/packages/iamfarhad-validation)[kiczort/polish-validator-bundle

Symfony bundle with validators for Polish identification numbers: PESEL, NIP, REGON, PWZ.

1223.2k](/packages/kiczort-polish-validator-bundle)

PHPackages © 2026

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