dev-master
9999999-devCreate labels for any model constants
GPLv3
The Requires
by Anton Korniychuk
Wallogit.com
2017 © Pedro Peláez
Create labels for any model constants
Feel free to let me know what else you want added via:, (*1)
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
$ php composer.phar require ancor/yii2-constant-label
or add, (*4)
"ancor/yii2-constant-label": "dev-master"
to the require section of your composer.json file., (*5)
To use ConstantLabelBehavior, insert the following code to your Model class:, (*6)
use common\behaviors\ConstantLabelBehavior;
/**
* @mixin ConstantLabelBehavior
*/
class MyModel extends Model
{
const STATUS_ACTIVE = 10;
const STATUS_DELETED = 0;
public function behaviors()
{
return [
[
'class' => ConstantLabelBehavior::className(),
'constantLabels' => [
'status' => [
self::STATUS_ACTIVE => 'User is active',
self::STATUS_DELETED => 'User deleted',
]
],
]
];
}
}
$model = new MyModel();
// return key-value array with constant values as key and constant label as value
$labels = $model->getConstantLabels('status');
// return label for one constant
$label = $model->getConstant('status', $model::STATUS_ACTIVE);
// return values of all constants
$values = $model->getConstantValues('status'); // [10, 0]
// also it can be use in validation rules
[
['status', 'in', 'range' => $model->getConstantValues('status')],
]
Create labels for any model constants
GPLv3