herroffizier / yii2-attribute-index-validator
Yii2 validator that adds incremental index to attribute values to make them unique.
Installs: 1 997
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Type:yii2-extension
pkg:composer/herroffizier/yii2-attribute-index-validator
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: 2025-10-25 22:50:31 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.
separatorsets separator between original value and index. Default separator is-.startIndexdefines start index. Default value is1.filterdefines 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 arrayfiltervalue will be passed to\yii\web\ActiveQueryInterface::andWhere()method. In case of anonymous function its signature must befunction($query)and instance of\yii\web\ActiveQueryInterfacewill be passed to it. Default value isnull.