herroffizier / yii2-attribute-index-validator
Yii2 validator that adds incremental index to attribute values to make them unique.
Installs: 1 993
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Type:yii2-extension
Requires
- yiisoft/yii2: *
Requires (Dev)
- codeception/codeception: 2.0.*
- codeception/specify: *
- codeception/verify: *
- phing/phing: *
- squizlabs/php_codesniffer: dev-master
- yiisoft/yii2-codeception: *
This package is not auto-updated.
Last update: 2024-12-21 17:27:54 UTC
README
This validator solves value collisions for unique model attributes by adding incremental index to repeating values. E.g. title
will become title-1
if item with title
already exists.
Such behavior may be useful for tasks like generating URLs and so on.
Installation
Install validator with Composer:
composer require --prefer-dist "herroffizier/yii2-attribute-index-validator:@stable"
Usage
Add validator to your model's rules array before required
and unique
validators (if any).
use herroffizier\yii2aiv\AttributeIndexValidator; ... public function rules() { return [ [['attribute'], AttributeIndexValidator::className()], [['attribute'], 'required'], [['attribute'], 'unique'], ]; }
Validator has a few options to customize its behavior.
separator
sets separator between original value and index. Default separator is-
.startIndex
defines start index. Default value is1
.filter
defines additional filter to be applied to query used to check attribute uniqueness. May be either a string, an array or an anonymous function. In case of string or arrayfilter
value will be passed to\yii\web\ActiveQueryInterface::andWhere()
method. In case of anonymous function its signature must befunction($query)
and instance of\yii\web\ActiveQueryInterface
will be passed to it. Default value isnull
.