PHPackages                             rasuvaeff/yii3-respect-validation - 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. rasuvaeff/yii3-respect-validation

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

rasuvaeff/yii3-respect-validation
=================================

Bridge exposing Respect/Validation rules as native yiisoft/validator rules for Yii3 applications

v1.0.1(1mo ago)00BSD-3-ClausePHPPHP &gt;=8.5CI passing

Since Jul 3Pushed 1mo agoCompare

[ Source](https://github.com/rasuvaeff/yii3-respect-validation)[ Packagist](https://packagist.org/packages/rasuvaeff/yii3-respect-validation)[ Docs](https://github.com/rasuvaeff/yii3-respect-validation)[ RSS](/packages/rasuvaeff-yii3-respect-validation/feed)WikiDiscussions master Synced 1w ago

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

rasuvaeff/yii3-respect-validation
=================================

[](#rasuvaeffyii3-respect-validation)

[![Stable Version](https://camo.githubusercontent.com/470e5d1b6c8b5a6f085adae68e4266490e5c89cc7c2267633726368cce742346/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7261737576616566662f796969332d726573706563742d76616c69646174696f6e3f6c6162656c3d737461626c6526736f72745f73656d7665723d31)](https://packagist.org/packages/rasuvaeff/yii3-respect-validation)[![Total Downloads](https://camo.githubusercontent.com/00ccd5a673cfaa342ff22da5813367a4314dc7ebd1be496e3b0b96a00e8126c9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7261737576616566662f796969332d726573706563742d76616c69646174696f6e)](https://packagist.org/packages/rasuvaeff/yii3-respect-validation)[![Build](https://camo.githubusercontent.com/4457c2e467b1ce07e7915bed0ea487ec332756ed58af79f2b8730ddb50f408b6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7261737576616566662f796969332d726573706563742d76616c69646174696f6e2f6275696c642e796d6c3f6272616e63683d6d6173746572)](https://github.com/rasuvaeff/yii3-respect-validation/actions)[![Static analysis](https://camo.githubusercontent.com/1ca1493dd9c59f3d4e1a7c7a95dae5139112177e4d0abc567195b6473e074e1d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7261737576616566662f796969332d726573706563742d76616c69646174696f6e2f7374617469632d616e616c797369732e796d6c3f6272616e63683d6d6173746572266c6162656c3d737461746963253230616e616c79736973)](https://github.com/rasuvaeff/yii3-respect-validation/actions)[![Psalm level](https://camo.githubusercontent.com/96f0376e917393dc04f5e50ef34fa74f202272a3100aa70c66ece3282f6a2e36/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7073616c6d2d6c6576656c253230312d3134314634383f6c6f676f3d7073616c6d266c6f676f436f6c6f723d7768697465)](https://github.com/rasuvaeff/yii3-respect-validation/blob/master/psalm.xml)[![PHP](https://camo.githubusercontent.com/2c4cbf0b7f5eedc13e0227d8c517ee1a4a7a956425c56448ca80b4e844bc4b09/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f7261737576616566662f796969332d726573706563742d76616c69646174696f6e2f706870)](https://packagist.org/packages/rasuvaeff/yii3-respect-validation)[![License](https://camo.githubusercontent.com/c32b711cf557d6bcfa4d13f8d30f6203cba5c00fe783d822d1eaf07c06c565d5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7261737576616566662f796969332d726573706563742d76616c69646174696f6e)](LICENSE.md)

Bridge exposing [Respect/Validation](https://github.com/Respect/Validation) rules as native `yiisoft/validator` rules — usable as a `#[RespectRule]` attribute on a `FormModel`/DTO property, alongside built-in Yii3 rules, with results going through the same `Result`/`Yiisoft\Translator` pipeline.

> **Using an AI coding assistant?** [llms.txt](llms.txt) contains a compact API reference you can share with the model. Contributors: see [AGENTS.md](AGENTS.md).

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

[](#requirements)

RequirementVersionPHP`>=8.5` (required by `respect/validation` ^3.1)`respect/validation``^3.1``yiisoft/validator``^2.6``yiisoft/translator``^3.0`Installation
------------

[](#installation)

```
composer require rasuvaeff/yii3-respect-validation
```

Usage
-----

[](#usage)

```
use Rasuvaeff\Yii3RespectValidation\RespectRule;
use Respect\Validation\Validators\AllOf;
use Respect\Validation\Validators\Between;
use Respect\Validation\Validators\Length;
use Respect\Validation\Validators\StringType;

final class RegisterForm
{
    #[RespectRule(new AllOf(new StringType(), new Length(new Between(1, 50))))]
    public string $username = '';
}
```

```
use Yiisoft\Validator\Validator;

$result = (new Validator())->validate(new RegisterForm());

$result->isValid();                          // false
$result->getPropertyErrorMessages('username'); // ['Username must be between 1 and 50']
```

`v::` fluent chains work too, since `RespectRule` accepts either a `Respect\Validation\Validator` instance or a `Respect\Validation\ValidatorBuilder`(what `v::` returns):

```
#[RespectRule(new StringType())]
public string $username = '';

// equivalent, built at runtime instead of in the attribute:
$rule = new RespectRule(v::stringType());
```

> PHP attribute arguments must be constant expressions, so only rule chains built from `new Validator(...)` calls work directly in `#[RespectRule(...)]`. `v::`chains need to be built outside the attribute and passed to the constructor.

### Fluent `v::` chains on a model: `RulesProviderInterface`

[](#fluent-v-chains-on-a-model-rulesproviderinterface)

To keep the fluent `v::` API without giving up per-model rule declarations, implement `Yiisoft\Validator\RulesProviderInterface` instead of attributes — `getRules()` is ordinary runtime code, so any chain works there:

```
use Respect\Validation\Validator as v;
use Yiisoft\Validator\RulesProviderInterface;

final class RegisterForm implements RulesProviderInterface
{
    public string $username = '';
    public string $email = '';

    public function getRules(): iterable
    {
        return [
            'username' => new RespectRule(v::stringType()->length(v::between(1, 50))),
            'email' => new RespectRule(v::email()),
        ];
    }
}
```

Both styles produce identical results and can be mixed: attributes for simple `new`-constructible chains, `getRules()` where the fluent builder is worth it.

### Skip / conditional validation

[](#skip--conditional-validation)

`RespectRule` implements the same `SkipOnEmptyInterface` / `SkipOnErrorInterface` / `WhenInterface` contracts as built-in rules:

```
#[RespectRule(new StringType(), skipOnEmpty: true, skipOnError: true)]
public string $bio = '';
```

### Messages and translation

[](#messages-and-translation)

Respect's own message templates (e.g. `{{subject}} must be a string`) are rendered by the handler itself — not by Respect's internal `InterpolationRenderer` (which needs `symfony/translation-contracts`) — so the whole app keeps a single i18n pipeline: `Yiisoft\Translator`.

By default (no `TranslatorInterface` wired), messages are rendered in English via `RespectMessageFormatter`.

**A complete Russian catalog ships with the package** (`messages/ru/`, all ~310 Respect template strings). To activate it, install the PHP message reader and set the application locale to `ru` — the shipped `config/di.php` picks it up automatically:

```
composer require yiisoft/translator-message-php
```

A build-time test (`RussianMessageCatalogTest`) pins every catalog key to the installed `respect/validation` templates, so an upstream wording change turns CI red instead of silently dropping translations.

For other locales, add your own catalog next to the app (same category):

```
// messages/de/yii3-respect-validation.php
return [
    '{{subject}} must be a string' => '{{subject}} muss eine Zeichenkette sein',
];
```

### DI configuration (Yii3)

[](#di-configuration-yii3)

This package ships `config/di.php` + `config/params.php` via `config-plugin` — installing it alongside `yiisoft/config` is enough to get `RespectRuleHandler`wired with a dedicated translation category (`yii3-respect-validation`) using a `{{placeholder}}`-aware `RespectMessageFormatter` (Respect templates use double braces, not the ICU syntax the rest of `yiisoft/validator` uses — mixing the two under one formatter would break placeholder substitution for one of them).

The category reads the bundled `messages/{locale}` catalogs when `yiisoft/translator-message-php` is installed; without it, messages pass through untranslated (English template text).

Override the category name in your application config if needed:

```
// config/params.php
return [
    'rasuvaeff/yii3-respect-validation' => [
        'translation.category' => 'yii3-respect-validation',
    ],
];
```

Components
----------

[](#components)

### `RespectRule`

[](#respectrule)

ParameterTypeDefaultDescription`validator``Respect\Validation\Validator|Respect\Validation\ValidatorBuilder`—The wrapped Respect rule/chain.`skipOnEmpty``bool|callable|null``null`Skip on empty, see `SkipOnEmptyInterface`.`skipOnError``bool``false`Skip on prior error, see `SkipOnErrorInterface`.`when``?Closure``null`Conditional execution, see `WhenInterface`.`getOptions()` (for frontend/client-side metadata export, per `DumpedRuleInterface`) returns the wrapped validator's own `$parameters` property when present (e.g. `Between` exposes `minValue`/`maxValue` — the same values Respect uses to fill in its own templates), plus `skipOnEmpty`/`skipOnError`. **No JavaScript runtime is shipped** — Respect has ~150 rules, many encoding arbitrary PHP logic (Luhn checksums, locale-aware checks, `Callback` closures); porting all of them to JS is not realistic to maintain, so this package limits itself to the same metadata-export mechanism `yiisoft/validator` itself uses.

### `RespectRuleHandler`

[](#respectrulehandler)

ParameterTypeDefaultDescription`translator``?Yiisoft\Translator\TranslatorInterface``null`If `null`, falls back to `RespectMessageFormatter` directly (English only).`translationCategory``string``'yii3-respect-validation'`Category looked up on `$translator`.Walks the `Respect\Validation\Result` tree returned by `evaluate()` and emits one `Yiisoft\Validator\Result` error per failed leaf (and per failed `adjacent`message), each with its own `valuePath` built from Respect's own nested `Path`(e.g. failing array keys under `Each`/`KeySet`).

### `RespectMessageFormatter`

[](#respectmessageformatter)

`Yiisoft\Translator\MessageFormatterInterface` implementation substituting Respect's own `{{param}}` placeholder syntax (not ICU). Register it on a `Yiisoft\Translator\CategorySource` — see [DI configuration](#di-configuration-yii3).

Security
--------

[](#security)

- No user input is ever interpolated into anything executed — this package only moves data between two validation result structures (Respect's `Result` tree and Yii3's `Result`); it performs no I/O, SQL, or shell access itself.
- `RespectMessageFormatter` only does `{{key}}` string substitution (`strtr()`) — never `eval`/`preg_replace` with the `/e` modifier or similar.

Examples
--------

[](#examples)

See [examples/](examples/) for a runnable script.

ScriptShowsNeeds server?[`validate-form.php`](examples/validate-form.php)Wrapping Respect rules on a DTO, reading `Result` errorsnoDevelopment
-----------

[](#development)

No PHP/Composer on the host — run in Docker via the `composer:2` image:

```
docker run --rm -v "$PWD":/app -w /app composer:2 composer install
docker run --rm -v "$PWD":/app -w /app composer:2 composer build
docker run --rm -v "$PWD":/app -w /app composer:2 composer cs:fix
docker run --rm -v "$PWD":/app -w /app composer:2 composer test
```

Or with Make:

```
make install
make build
make cs-fix
make test
```

CI runs `composer build` on PHP 8.5 only — `respect/validation` requires `php: >=8.5`, so the usual 8.3/8.4/8.5 matrix does not apply to this package.

License
-------

[](#license)

[BSD-3-Clause](LICENSE.md)

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance90

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community6

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

Every ~0 days

Total

2

Last Release

48d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b0812d5572a7041dfe36e222d295b2e6dc55833a605350fcde58a51a5965ed30?d=identicon)[rasuvaeff](/maintainers/rasuvaeff)

---

Top Contributors

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

---

Tags

attributeform-validationphprespect-validationvalidationyii3validatorvalidationrespectyii3respect validation

###  Code Quality

Static AnalysisPsalm, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/rasuvaeff-yii3-respect-validation/health.svg)

```
[![Health](https://phpackages.com/badges/rasuvaeff-yii3-respect-validation/health.svg)](https://phpackages.com/packages/rasuvaeff-yii3-respect-validation)
```

###  Alternatives

[respect/validation

The most awesome validation engine ever created for PHP

6.0k40.8M433](/packages/respect-validation)[opis/json-schema

Json Schema Validator for PHP

65446.2M369](/packages/opis-json-schema)[awurth/slim-validation

A wrapper around the respect/validation PHP validation library for easier error handling and display

64406.0k10](/packages/awurth-slim-validation)[workerman/validation

The most awesome validation engine ever created for PHP. Respect/Validation 汉化版本

2337.5k9](/packages/workerman-validation)[resultsystems/validation

Inspired 'KennedyTedesco Validation' - The power of 'Respect Validation' on Laravel.

2832.9k4](/packages/resultsystems-validation)

PHPackages © 2026

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