PHPackages                             carono/yii2-rule-helper - 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. carono/yii2-rule-helper

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

carono/yii2-rule-helper
=======================

Yii2 helper for working with model validation rules and validators. Provides methods to check if validators are applied to model attributes and get validator short names

1.0.0(10mo ago)02MITPHPPHP ^7.4 || 8.\*

Since Aug 26Pushed 10mo agoCompare

[ Source](https://github.com/carono/yii2-rule-helper)[ Packagist](https://packagist.org/packages/carono/yii2-rule-helper)[ RSS](/packages/carono-yii2-rule-helper/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (2)Used By (0)

Yii2 Rule Helper
================

[](#yii2-rule-helper)

A lightweight helper module for Yii2 framework that provides utility methods for working with model validation rules and validators.

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

[](#installation)

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

Either run:

```
composer require carono/yii2-rule-helper
```

or add:

```
"carono/yii2-rule-helper": "*"
```

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

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

[](#requirements)

- PHP 7.1 or higher
- Yii2 framework

Usage
-----

[](#usage)

### `RuleHelper::getValidatorShortName($validator)`

[](#rulehelpergetvalidatorshortnamevalidator)

Gets the short name of a validator from Yii2's built-in validators.

```
use carono\yii2\helpers\RuleHelper;

// Returns 'required'
$shortName = RuleHelper::getValidatorShortName('yii\validators\RequiredValidator');

// Returns 'string'
$shortName = RuleHelper::getValidatorShortName('yii\validators\StringValidator');

// Returns null for non-built-in validators
$shortName = RuleHelper::getValidatorShortName('app\validators\CustomValidator');
```

### `RuleHelper::haveValidator($model, string $attribute, $validator)`

[](#rulehelperhavevalidatormodel-string-attribute-validator)

Checks if a model has a specific validator applied to a given attribute.

```
use carono\yii2\helpers\RuleHelper;
use app\models\User;

// Using validator class name
$hasRequired = RuleHelper::haveValidator(User::class, 'username', 'yii\validators\RequiredValidator');

// Using validator short name
$hasString = RuleHelper::haveValidator($userModel, 'email', 'string');

// Using validator instance (for custom validators)
$customValidator = new CustomValidator();
$hasCustom = RuleHelper::haveValidator($userModel, 'custom_field', $customValidator);
```

#### Parameters

[](#parameters)

- `$model`: Can be either a Model instance or a class name that extends `yii\base\Model`
- `$attribute`: The attribute name to check
- `$validator`: Can be:
    - Validator class name (e.g., `'yii\validators\RequiredValidator'`)
    - Validator short name (e.g., `'required'`, `'string'`)
    - Validator instance

#### Return Value

[](#return-value)

Returns `true` if the specified validator is applied to the given attribute, `false` otherwise.

Examples
--------

[](#examples)

### Checking for specific validators

[](#checking-for-specific-validators)

```
class User extends \yii\db\ActiveRecord
{
    public function rules()
    {
        return [
            [['username', 'email'], 'required'],
            ['email', 'email'],
            ['username', 'string', 'max' => 255],
        ];
    }
}

// Check validators
$hasRequired = RuleHelper::haveValidator(User::class, 'username', 'required'); // true
$hasEmailValidator = RuleHelper::haveValidator(User::class, 'email', 'email'); // true
$hasNumberValidator = RuleHelper::haveValidator(User::class, 'username', 'number'); // false
```

### Using in controller or component

[](#using-in-controller-or-component)

```
public function actionCheckValidators($id)
{
    $user = User::findOne($id);

    if (RuleHelper::haveValidator($user, 'email', 'required')) {
        // Handle required email field
    }

    if (RuleHelper::haveValidator($user, 'username', 'string')) {
        // Handle string validation for username
    }
}
```

License
-------

[](#license)

MIT

Support
-------

[](#support)

If you have any questions or issues, please create an issue on GitHub.

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

[](#contributing)

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance54

Moderate activity, may be stable

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

312d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6193985?v=4)[Александр Касьянов](/maintainers/carono)[@carono](https://github.com/carono)

---

Top Contributors

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

---

Tags

yii2extension

### Embed Badge

![Health badge](/badges/carono-yii2-rule-helper/health.svg)

```
[![Health](https://phpackages.com/badges/carono-yii2-rule-helper/health.svg)](https://phpackages.com/packages/carono-yii2-rule-helper)
```

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M3.1k](/packages/craftcms-cms)[skeeks/cms

SkeekS CMS — control panel and tools based on php framework Yii2

13825.8k59](/packages/skeeks-cms)[kdn/yii2-domain-validator

Domain name validator for Yii 2.

11125.8k1](/packages/kdn-yii2-domain-validator)[nepstor/yii2-datetime-compare-validator

Yii2 validator for compare datetime.

1285.6k1](/packages/nepstor-yii2-datetime-compare-validator)

PHPackages © 2026

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