PHPackages                             faryshta/yii2-enum - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. faryshta/yii2-enum

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

faryshta/yii2-enum
==================

Enum classes for Yii2 models and forms

1.0.0(10y ago)15332.9k↓30.7%3[1 issues](https://github.com/Faryshta/yii2-enum/issues)BSD-3-ClausePHPPHP &gt;=5.4.0

Since Nov 28Pushed 9y ago3 watchersCompare

[ Source](https://github.com/Faryshta/yii2-enum)[ Packagist](https://packagist.org/packages/faryshta/yii2-enum)[ Docs](http://www.yiiframework.com/)[ RSS](/packages/faryshta-yii2-enum/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

Faryshta Yii2 Enum
==================

[](#faryshta-yii2-enum)

[![Latest Stable Version](https://camo.githubusercontent.com/c0ccc0e110307932882b989219e69ddd754436a42b96ba16e222dd9c7b510eeb/68747470733a2f2f706f7365722e707567782e6f72672f66617279736874612f796969322d656e756d2f762f737461626c65)](https://packagist.org/packages/faryshta/yii2-enum) [![Total Downloads](https://camo.githubusercontent.com/b3e1c8838ed17734447a91f7942000ecb07dd9e3b2adebd74c5137af0b57c1e5/68747470733a2f2f706f7365722e707567782e6f72672f66617279736874612f796969322d656e756d2f646f776e6c6f616473)](https://packagist.org/packages/faryshta/yii2-enum) [![Latest Unstable Version](https://camo.githubusercontent.com/07f55f21f9c1aaaf8239dd217486f391c3f23b36d60e7f207721dbdf2294243b/68747470733a2f2f706f7365722e707567782e6f72672f66617279736874612f796969322d656e756d2f762f756e737461626c65)](https://packagist.org/packages/faryshta/yii2-enum) [![License](https://camo.githubusercontent.com/9cda5db428c9da02cc2cf7c88eb0a3031641de3656816472e8072cb244633049/68747470733a2f2f706f7365722e707567782e6f72672f66617279736874612f796969322d656e756d2f6c6963656e7365)](https://packagist.org/packages/faryshta/yii2-enum)

Faryshta Yii2 Enum extension provides support for the ussage of enumarions in Yii2 models and forms.

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/). Check the [composer.json](https://github.com/Faryshta/yii2-enum/blob/master/composer.json) for this extension's requirements and dependencies.

To install, either run

```
$ php composer.phar require faryshta/yii2-enum "@dev"

```

or add

```
"faryshta/yii2-enum": "@dev "

```

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

Usage
-----

[](#usage)

### EnumTrait

[](#enumtrait)

```
use faryshta\base\EnumTrait;

class Person extends \yii\base\Model
{
    use EnumTrait;

    public static function enums()
    {
        return [
            // this is the name of the enum.
            'gender' => [
                // here it follows the `'index' => 'desc'` notation
                'F' => 'Female',
                'M' => 'Male',
            ],
        ];
    }

    // optional magic method to access the value quickly
    public function getGenderDesc()
    {
        // method provided in the EnumTrait to get the description of the value
        // of the attribute
        return $this->getAttributeDesc('gender');
    }
}
```

### EnumValidator

[](#enumvalidator)

```
use faryshta\base\EnumTrait;
use faryshta\validators\EnumValidator;

class Person extends \yii\base\Model
{
    use EnumTrait;

    public $gender;

    public static function enums()
    {
        return [
            // this is the name of the enum.
            'gender' => [
                // here it follows the `'index' => 'desc'` notation
                'F' => 'Female',
                'M' => 'Male',
            ],
        ];
    }

    public function rules()
    {
        return [
            [
                ['gender'],
                EnumValidator::className(),

                // optional, if you want to use a diferent class than the
                // class of the current model
                // 'enumClass' => Person::className()

                // optional, if you want to use a diferent enum name than the
                // name of the attribute being validated
                // 'enumName' => 'gender'
            ],
        ];
    }
}
```

### Enum Widgets

[](#enum-widgets)

In a view file

```
use faryshta\widgets\EnumDropdown;
use faryshta\widgets\EnumRadio;

/**
 * @var Person $model
 * @var ActiveForm $form
 */

// with ActiveForm
echo $form->field($person, 'gender')->widget(EnumDropdown::className());

// without ActiveForm and with model.
echo EnumDropdown::widget([
    'model' => $person,
    'attribute' => 'gender',
]);

// without Model
echo EnumDropdown::widget([
    'name' => 'gender',
    'enumClass' => Person::className(),
    'enumName' => 'gender',
]);

// The same applies for the EnumRadio widget if you want to render a
// list of radio buttons
echo $form->field($person, 'gender')->widget(EnumRadio::className());
```

### EnumColumn

[](#enumcolumn)

In a view file

```
use faryshta\data\EnumColumn;
use yii\widgets\GridView;

echo GridView::widget([
    'searchModel' => $personSearch,
    'dataProvider' => $personDataProvider,
    'columns' => [
        'class' => EnumColumn::className(),
        'attribute' => 'gender',

        // optional, if you want to use a diferent class than the
        // class of the current model
        // 'enumClass' => Person::className()

        // optional, if you want to use a diferent enum name than the
        // name of the attribute being validated
        // 'enumName' => 'gender'
    ],
]);
```

License
-------

[](#license)

**Faryshta Yii2 Enum** is released under the BSD 3-Clause License. See the bundled `LICENSE.md` for details.

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity44

Moderate usage in the ecosystem

Community12

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.6% 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

3824d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3d5f64412ef020f8c137ff5c7f5e4a0866271f2f9ba9584e5a24aa48467f958d?d=identicon)[Faryshta](/maintainers/Faryshta)

---

Top Contributors

[![Faryshta](https://avatars.githubusercontent.com/u/2029247?v=4)](https://github.com/Faryshta "Faryshta (35 commits)")[![FopherC](https://avatars.githubusercontent.com/u/1615875?v=4)](https://github.com/FopherC "FopherC (2 commits)")

---

Tags

enumyii2

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/faryshta-yii2-enum/health.svg)

```
[![Health](https://phpackages.com/badges/faryshta-yii2-enum/health.svg)](https://phpackages.com/packages/faryshta-yii2-enum)
```

###  Alternatives

[dmstr/yii2-cookie-consent

Yii2 Cookie Consent Widget

1452.6k](/packages/dmstr-yii2-cookie-consent)

PHPackages © 2026

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