poalrom/exclusive-pack

Pack of validator and behavior for work with exclusive values in AR used Yii2

1.1.0 2017-01-12 12:03 UTC

README

Pack of validator and behavior for work with exclusive values in AR used Yii2

Install

Use composer command for installing this package:

composer require poalrom/exclusive-pack

How to use

Validator

Use ExclusiveValidator like each other validator in Yii2. For example:

namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use poalrom\exclusive-pack\validators\ExclusiveValidator;

class AttributeValue extends ActiveRecord
{
    public $value;
    public $is_default;

    public function rules()
    {
        return [
            [['value'], 'required'],
            ['is_default', ExclusiveValidator::className(), 'exclusiveValues' => [1]],
        ];
    }
}

In this example we set "Attribute can have only one value with is_default = 1".

Full list of options:

  • exclusiveValues - Array of exclusive values. Each of this values can be assign to only one record. Required.
  • targetClass - ClassName, where we find record. If null or not set, find records in validating model's class.
  • message - Error message, if we find another row with same exclusive value.
  • groupAttributes - Array of attribute names, using for grouping records. If set, search find only records with same values of grouping attributes as validating record.

Example of use options:

/**
 * @property int    $id
 * @property int    $attribute_id
 * @property int    $is_default
 * @property string $value
 * @package app\models
 */
class AttributeValue extends ActiveRecord
{
    public static function tableName()
    {
        return 'attribute_value';
    }

    public function rules()
    {
        return [
            ['is_default', ExclusiveValidator::className(), 'exclusiveValues' => [1], 'message' => 'This attribute already have default value', 'groupAttributes' => ['product_attribute_id']],
            [['product_attribute_id', 'lang_id', 'is_default'], 'integer'],
            [['value'], 'string'],
        ];
    }

Behavior

May be used only with ActiveRecord instances.
Behavior for simplify work with ExclusiveValidator.
Automaticly change other records in group, if model want to set exclusive value to target attribute.
Use like other behaviors in Yii2. Simple example:

public function behaviors()
    {
        return [
            [
                'class'           => ExclusiveValues::className(),
                'attributes'      => [
                    static::EVENT_BEFORE_VALIDATE => 'is_default',
                ],
                'defaultValues'   => [
                    'is_default' => 0,
                ],
                'groupAttributes' => [
                    'is_default' => ['product_attribute_id'],
                ],
                'exclusiveValues' => [
                    'is_default' => [1],
                ],
            ],
        ];
    }

Full list of options:

  • attributes - Array of attributes for watching. Keys are event name of owner model. Values are strings or arrays of strings with attribute titles. Required
  • defaultValues - Array of default values for each of watched attributes. Required
  • exclusiveValues - Array of exclusive values. Each of this values can be assign to only one record. Required.
  • groupAttributes - Array of attribute names, using for grouping records. If set, update only records with same values of grouping attributes as updating record.