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.1.0(1mo ago)22MITPHPPHP &gt;=8.4CI passing

Since Mar 13Pushed 1mo 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 today

READMEChangelog (5)Dependencies (14)Versions (8)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)
- [When to use it](#when-to-use-it)
- [Usage](#usage)
- [Constraints](#constraints)
- [Methods](#methods)
    - [validate](#validate)
    - [assert](#assert)
    - [isValid](#isvalid)
    - [toArray](#toarray)
    - [addNamespace](#addnamespace)
    - [setTranslator](#settranslator)
    - [reset](#reset)
- [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
```

When to use it
--------------

[](#when-to-use-it)

Use Fluent Validator when you want Symfony Validator constraints for raw values without setting up object metadata, attributes, forms, or a larger validation layer. It is useful for small input checks, command arguments, request fragments, webhook payload values, configuration values, and library code.

This package does not replace Symfony Validator. It wraps Symfony Validator and keeps its constraints, violation objects, groups, translations, and custom constraint model.

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
}
```

Use `assert` when invalid values should stop the current flow:

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

try {
    Validator::notBlank()->email()->assert($email, 'email');
}
catch (ValidationFailedException $exception) {
    $message = $exception->getMessage();
    // "email: This value is not a valid email address."
}
```

Use `isValid` when you only need a boolean:

```
use ProgrammatorDev\FluentValidator\Validator;

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

Constraint autocompletion is available in IDEs like PhpStorm. The suggested methods are generated from the installed Symfony Validator constraints. 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).

### Groups

[](#groups)

Validation groups work the same way as in Symfony Validator:

```
use ProgrammatorDev\FluentValidator\Validator;

$validator = Validator::notBlank(groups: ['Default'])
    ->email(groups: ['registration']);

$validator->isValid('invalid-email', groups: ['Default']); // true
$validator->isValid('invalid-email', groups: ['registration']); // false
```

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.

### `reset`

[](#reset)

```
reset(): void
```

Clears globally registered custom constraint namespaces and translator configuration. Useful when changing global validator configuration in tests, workers, or other long-running PHP processes.

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

44

—

FairBetter than 90% of packages

Maintenance93

Actively maintained with recent releases

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity60

Established project with proven stability

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

Recently: every ~106 days

Total

6

Last Release

35d ago

Major Versions

v0.3.0 → v1.0.02025-12-08

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

v1.0.0PHP &gt;=8.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/136833732?v=4)[Programmator](/maintainers/programmatordev)[@programmatordev](https://github.com/programmatordev)

---

Top Contributors

[![andrepimpao](https://avatars.githubusercontent.com/u/41913452?v=4)](https://github.com/andrepimpao "andrepimpao (59 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

[easycorp/easyadmin-bundle

Admin generator for Symfony applications

4.3k17.9M387](/packages/easycorp-easyadmin-bundle)[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.5k5.9M738](/packages/sylius-sylius)[pimcore/pimcore

Content &amp; Product Management Framework (CMS/PIM/E-Commerce)

3.8k3.8M508](/packages/pimcore-pimcore)[shopware/platform

The Shopware e-commerce core

3.4k1.5M3](/packages/shopware-platform)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

585.6M574](/packages/shopware-core)[typo3/cms

TYPO3 CMS is a free open source Content Management Framework initially created by Kasper Skaarhoj and licensed under GNU/GPL.

1.2k1.9M122](/packages/typo3-cms)

PHPackages © 2026

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