ezijing / yii2-validator
Yii2的参数验证器,兼容model的rules;减少验证参数的时候要新建model的烦恼
Installs: 361
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- php: >=7.0
- yiisoft/yii2: ~2.0.14
Requires (Dev)
Replaces
- bower-asset/inputmask: >=3.2.0
- bower-asset/jquery: >=1.11.0
- bower-asset/punycode: >=1.3.0
- bower-asset/yii2-pjax: >=2.0.0
README
Yii2参数验证
1. 添加配置信息
common/config/main.php
<?php return [ 'id' => 'crontab-console', 'basePath' => dirname(__DIR__), 'controllerNamespace' => 'app\commands', 'timeZone' => 'Asia/Shanghai', 'aliases' => [ '@bower' => '@vendor/bower-asset', '@npm' => '@vendor/npm-asset', ], // 配置依赖关系 'container' => [ 'definitions' => [ 'King\Yii2Validator\ValidatorModelInterface' => 'King\Yii2Validator\model\ValidatorModel', 'King\Yii2Validator\Validator' => 'King\Yii2Validator\model\ParamsValidator', ], 'singletons' => [ ], ], // 配置组件 'components' => [ 'validator' => 'King\Yii2Validator\Validator', ] ];
2. 使用
依赖注入方式
controller中使用
<?php use components\validator\Validator; /** * 微信. */ class TestController extends \yii\rest\Controller { public function actionAsyncOfficialAccountUsers(Validator $validator) { $data = $this->request->get(); $rules = [ [['appid', 'experiment_id', 'connection_id'], 'required'], [['experiment_id', 'connection_id'], 'number'], [['appid'], 'string'], ]; list($res, $errMsg) = $validator->validate($data, $rules); } }
组件方式
<?php /* @var \King\Yii2Validator\model\ParamsValidator $validator */ $validator = \Yii::$app->validator; $data = [ 'appid' => 'tesdasda', ]; $rules = [ [['appid', 'experiment_id', 'connection_id'], 'required'], [['experiment_id', 'connection_id'], 'number'], [['appid'], 'string'], ]; list($res, $errMsg) = $validator->validate($data, $rules);