petrabarus / yii2-switchcasevalidator
在类似switch-case的条件语句上运行的验证器
1.0.1
2015-09-30 03:47 UTC
Requires
- php: >=5.5.0
- yiisoft/yii2: *
Requires (Dev)
- phpunit/phpunit: 4.6.*
This package is auto-updated.
Last update: 2024-09-22 15:51:32 UTC
README
在类似switch-case的条件语句上运行的验证器
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
php composer.phar require --prefer-dist petrabarus/yii2-switchcasevalidator "*"
或将以下内容添加到您的 composer.json
文件的 require 部分:
"petrabarus/yii2-switchcasevalidator": "*"
要求
此包需要
- 最新版的 Yii2
- PHP 5.4 或更高版本
使用方法
在模型的 rules()
方法中添加类似以下的内容。
//Assuming the model has attribute case, field1, and field2. //Each rule group will be validated when the case attribute match the cases. public function rules() { ['case', PetraBarus\Yii2\SwitchCaseValidator\Validator::class, //For PHP 5.4, you can use PetraBarus\Yii2\SwitchCaseValidator\Validator::className() or // string 'PetraBarus\Yii2\SwitchCaseValidator\Validator' 'cases' => [ 1 => [ ['field1', 'required'], ], 2 => [ ['field1', 'compare', 'compareValue' => 'Test'] ], 3 => [ ['field1', 'compare', 'compareValue' => 'Value 1'], ['field2', 'email'] ] ], //This will be executed if no cases matches. 'default' => [ ['field1', 'compare', 'compareValue' => 'Value 2'], ['field1', 'compare', 'compareValue' => 'Value 4'], ] ] }
或者查看测试文件。
测试
要运行测试,执行以下命令:
$ phpunit