herroffizier / yii2-attribute-index-validator
Yii2 验证器,为属性值添加递增索引以使它们唯一。
1.0.0
2016-03-29 13:15 UTC
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-09-14 16:03:22 UTC
README
此验证器通过为重复值添加递增索引来解决唯一模型属性的价值冲突。例如,如果已存在具有
此类行为可能对生成URL等任务很有用。
安装
使用Composer安装验证器
composer require --prefer-dist "herroffizier/yii2-attribute-index-validator:@stable"
使用方法
在模型规则数组中添加验证器,在required
和unique
验证器(如果有)之前。
use herroffizier\yii2aiv\AttributeIndexValidator; ... public function rules() { return [ [['attribute'], AttributeIndexValidator::className()], [['attribute'], 'required'], [['attribute'], 'unique'], ]; }
验证器有几个选项可以自定义其行为。
separator
设置原始值和索引之间的分隔符。默认分隔符是-
。startIndex
定义起始索引。默认值是1
。filter
定义应用于检查属性唯一性的查询的额外过滤器。可以是字符串、数组或匿名函数。在字符串或数组的情况下,filter
值将传递给\yii\web\ActiveQueryInterface::andWhere()
方法。在匿名函数的情况下,其签名必须是function($query)
,并将\yii\web\ActiveQueryInterface
实例传递给它。默认值是null
。