PHPackages                             kop/yii2-conditional-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. kop/yii2-conditional-validator

AbandonedYii2-extension[Validation &amp; Sanitization](/categories/validation)

kop/yii2-conditional-validator
==============================

If-then validation rules for Yii2 Framework

145.0k5PHP

Since Dec 2Pushed 11y ago3 watchersCompare

[ Source](https://github.com/kop/yii2-conditional-validator)[ Packagist](https://packagist.org/packages/kop/yii2-conditional-validator)[ RSS](/packages/kop-yii2-conditional-validator/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Yii2 Conditional Validator
==========================

[](#yii2-conditional-validator)

> Note: Since version `2.0.0-beta`, [Yii2 has it's own conditional validator](http://www.yiiframework.com/doc-2.0/guide-input-validation.html#conditional-validation) built in to the core. Please use validator provided by the framework. This extension **will no receive updates any longer**.

Yii2 Conditional Validator (Y2CV) validates some attributes depending on certain conditions (rules). You can use any core validator as you usually would do or any other class based or inline validator. An interesting feature is that you can even use the own Y2CV inside itself to perform more complex conditions. Basically, Y2CV executes the rules set in the param `if` and if there are no errors executes the rules set in the param `then`.

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

[](#requirements)

- Yii 2.0
- PHP 5.4

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

[](#installation)

The preferred way to install this extension is through [Composer](http://getcomposer.org/).

Either run

`php composer.phar require kop/yii2-conditional-validator "dev-master"`

or add

` "kop/yii2-conditional-validator": "dev-master"`

to the `require` section of your `composer.json` file.

Syntax Example
--------------

[](#syntax-example)

```
[['safeAttributes'], `path.to.ConditionalValidator`,
    'if' => [
        // rule1: [['attrX', 'attrY'], 'required', ... ]
        // ruleN: ...
    ],
    'then' => [
        // rule1: [['attrZ', 'attrG'], 'required', ... ]
        // ruleN: ...
    ]
]
```

- `safeAttributes`: The name of the attributes that should be turned safe (since Yii has no way to make dynamic validators to turn attributes safe);
- `path.to.ConditionalValidator`: In the most of cases will be `ConditionalValidator::className()`;
- `if`: (bidimensional array) The conditional rules to be validated. *Only* if they are all valid (i.e., have no errors) then the rules in `then` will be validated;
- `then`: (bidimensional array) The rules that will be validated *only* if there are no errors in rules of `if` param.

> Note: Errors in the rules set in the param `if` are discarded after checking. Only errors in the rules set in param `then` are really kept.

Usage Examples
--------------

[](#usage-examples)

`If` *customer\_type* is "active" `then` *birthdate* and *city* are `required`:

```
public function rules()
{
    return [
        [['customer_type'], ConditionalValidator::className(),
            'if' => [
                [['customer_type'], 'compare', 'compareValue' => 'active']
            ],
            'then' => [
                [['birthdate', 'city'], 'required']
            ]
        ]
    ];
}
```

`If` *customer\_type* is "inactive" `then` *birthdate* and *city* are `required` **and** *city* must be "sao\_paulo", "sumare" or "jacarezinho":

```
public function rules()
{
    return [
        [['customer_type'], ConditionalValidator::className(),
            'if' => [
                [['customer_type'], 'compare', 'compareValue' => 'active']
            ),
            'then' => [
                [['birthdate', 'city'], 'required'],
                [['city'], 'in', 'range' => ['sao_paulo', 'sumare', 'jacarezinho']]
            ]
        ]
    ];
}
```

`If` *information* starts with 'http://' **and** has at least 24 chars length `then` the own *information* must be a valid url:

```
public function rules()
{
    return [
        [['information'], ConditionalValidator::className(),
            'if' => [
                [['information'], 'match', 'pattern' => '/^http:\/\//'],
                [['information'], 'string', 'min' => 24, 'allowEmpty' => false]
            ),
            'then' => [
                [['information'], 'url']
            ]
        ]
    ];
}
```

License
-------

[](#license)

**yii2-conditional-validator** is released under the MIT License. See the bundled `LICENSE.md` for details.

Resources
---------

[](#resources)

- [Project Page](http://kop.github.io/yii2-conditional-validator)
- [Packagist Package](https://packagist.org/packages/kop/yii2-conditional-validator)
- [Source Code](https://github.com/kop/yii2-conditional-validator)

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/645148?v=4)[Ivan Koptiev](/maintainers/kop)[@kop](https://github.com/kop)

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/kop-yii2-conditional-validator/health.svg)

```
[![Health](https://phpackages.com/badges/kop-yii2-conditional-validator/health.svg)](https://phpackages.com/packages/kop-yii2-conditional-validator)
```

###  Alternatives

[chaoswey/taiwan-id-validator

台灣身分證、統一編號驗證

319.9k](/packages/chaoswey-taiwan-id-validator)

PHPackages © 2026

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