使用方法(实现效果)
namespace frontend\controllers\api;
use yii\web\Controller;
use common\services\app\ParamsValidateService;
class ArticleController extends Controller
{
// 文章列表
public function actionList()
{
$PVS = new ParamsValidateService();
$valid = $PVS->validate(\Yii::$app->request->get(), [
['category_id', 'required'],
['category_id', 'integer'],
['keyword', 'string'],
]);
if (!$valid) {
$this->apiError(1001, $PVS->getErrorSummary(true));
}
//...
}
// 新增文章
public function actionPost()
{
$PVS = new ParamsValidateService();
$valid = $PVS->validate(\Yii::$app->request->get(), [
[['category_id', 'title', 'content'], 'required'],
['category_id', 'integer'],
[['title'], 'string', 'max' => 64],
[['content'], 'string'],
]);
if (!$valid) {
$this->apiError(1001, $PVS->getErrorSummary(true));
}
//...
}
// 文章删除
public function actionDelete()
{
$PVS = new ParamsValidateService();
$valid = $PVS->validate(\Yii::$app->request->get(), [
['article_id', 'required'],
['article_id', 'integer'],
]);
if (!$valid) {
$this->apiError(1001, $PVS->getErrorSummary(true));
}
//...
}
}实现方法
定义参数验证模型
定义参数验证模型 ParamsValidateModel ,继承 yii\db\ActiveRecord ,重写 attributes() 方法,主要功能:
验证规则可从对象外部进行设置。
从验证规则中获取可赋值的属性。
_rules = $rules;
foreach ($rules as $item) {
$this->_attributes = array_unique(array_merge($this->_attributes, (array)$item[0]));
}
}
// 重写获取验证规则
public function rules()
{
return $this->_rules;
}
// 设置可用属性列表
public function attributes()
{
return $this->_attributes;
}
}定义参数验证服务类
定义参数验证服务类,主要功能有:
设置参数列表和参数规则列表。
使用 参数验证模型 进行验证和存储验证错误消息。
使用魔术方法获取 参数验证模型 中的验证错误消息。
model = new ParamsValidateModel();
}
/**
* @param array $data 数据项
* @param array $rules 验证规则
* @return bool
*/
public function validate($data, $rules)
{
// 添加验证规则
$this->model->setRules($rules);
// 设置参数
$this->model->load($data, '');
// 进行验证
return $this->model->validate();
}
public function __call($name, $params)
{
if ($this->model->hasMethod($name)) {
return call_user_func_array([$this->model, $name], $params);
} else {
return parent::__call($name, $params);
}
}
}