Code Monkey home page Code Monkey logo

yii2-enum's Introduction

Faryshta Yii2 Enum

Latest Stable Version Total Downloads Latest Unstable Version License

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

Installation

The preferred way to install this extension is through composer. Check the 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

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

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

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

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

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

yii2-enum's People

Contributors

chofoteddy avatar faryshta avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

yii2-enum's Issues

Can EnumColumn be used with ListView

Your EnumColumn component works just great in yii\grid\GridView (maybe with this slight problem), but I totally can't force it to work with yii\widgets\DetailView. Is it possible at all (you don't have such solution among examples)?

I am using it the same way as in case of GridView (with this slight addition mentioned here):

<?= DetailView::widget([
    'model' => $model,
    'attributes' => [
        'id',
        'to',
        'from',
        [
            'attribute' => 'type',
            'class' => EnumColumn::class,
            'enumClass' => Command::class,
        ],
        'type',
        [
            'attribute' => 'status',
            'class' => EnumColumn::class,
            'enumClass' => Command::class,
        ],
        'status',
    ],
]) ?>

But for some reason columns with enum are rendered exactly in the same way:

image

No matter, if they're rendered by your extension or by Yii 2 internally.

Any idea what is wrong? Thank you.

EnumColumn::enumClass is required even if model is the same?

First of all thank you for a great set of components. Unfortunately, I have an issue around EnumColumn.

In GridView I must provide enumClass even if using the same model:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        [
            'attribute' => 'type',
            'class' => EnumColumn::class,
            'enumClass' => Command::class,
        ],

I am getting:

get_class(): Argument #1 ($object) must be of type object, null given

Given the fact that my model is the same:

return $this->render('index', [
    'dataProvider' => new ActiveDataProvider([
        'query' => Command::find(),
        'pagination' => ['pageSize' => 10],
        'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]],
    ]),
    'serverName' => Device::findOne(1)->name,
]);

Therefore, I am using the same model (Command) for both data provider and column class. So, according to your words:

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

This shouldn't be required. But, it is. At least on my side. I don't know, if this is a problem with my code or some bug in your code or some misunderstanding on my side.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.