[实践OK]easyswoole验证器,如何在控制器使用验证例子(demo)

jackxiang 2020-10-8 22:51 | |
验证器:
https://www.bookstack.cn/read/easyswoole-3.x-zh/Components-validate.md
App/HttpController/Index.php



App/HttpController/BaseController.php



统一验证:
class Index  extends BaseController
{
    function index()
    {
        $this->response()->write("123");
    }

  /**
     * 验证
     *
     * {@inheritdoc}
     * @see \App\HttpController\Index\Index::validateRule()
     */
    protected function validateRule(?string $action): ?Validate
    {
        $v = new Validate();
        $params = $this->request()->getRequestParam();
        echo "Here:\n";
        var_dump($action);
        switch ($action) {
            case 'index':
                {
                    $v->addColumn('title', '竞赛标题')
                        ->required('不能为空')
                        ->notEmpty();
                    $v->addColumn('limit', '每页条数')
                        ->integer();
                    $v->addColumn('page', '当前页码')
                        ->integer();
                    if (isset($params['recommend_status'])) {
                        $v->addColumn('recommend_status', '推荐状态')->integer();
                    }
                    if (isset($params['act_status'])) {
                        $v->addColumn('act_status', '活动状态')->integer();
                    }
                    break;
                }
            case 'searchActivity':
                {
                    $v->addColumn('title', '竞赛标题')
                        ->required('不能为空')
                        ->notEmpty();
                    $v->addColumn('limit', '每页条数')
                        ->integer();
                    $v->addColumn('page', '当前页码')
                        ->integer();
                    if (isset($params['recommend_status'])) {
                        $v->addColumn('recommend_status', '推荐状态')->integer();
                    }
                    if (isset($params['act_status'])) {
                        $v->addColumn('act_status', '活动状态')->integer();
                    }
                    break;
                }
            case 'getRobotAnswer':
                {
                    $v->addColumn('topic_id', '题目id')
                        ->required('不能为空')
                        ->integer();
                    $v->addColumn('score', '积分')
                        ->integer();
                    break;
                }
            case 'saveActivity':
                {
                    $v->addColumn('title', '竞赛标题')
                        ->required('不能为空')
                        ->notEmpty();
                    $v->addColumn('type', '展示模式')
                        ->required('不能为空')
                        ->integer();
                    $v->addColumn('description', '竞赛描述')
                        ->required('不能为空')
                        ->notEmpty();
                    $v->addColumn('start_time', '开始时间')
                        ->required('不能为空')
                        ->integer();
                    $v->addColumn('end_time', '结束时间')
                        ->required('不能为空')
                        ->integer();
                    $v->addColumn('paper_status', '试卷状态')
                        ->required('不能为空')
                        ->integer();
                    $v->addColumn('act_status', '活动状态')
                        ->required('不能为空')
                        ->integer();
                    if (isset($params['act_id'])) {
                        $v->addColumn('act_id', '竞赛id')->integer();
                    }
                    if (isset($params['recommend_status'])) {
                        $v->addColumn('recommend_status', '推荐状态')->integer();
                    }
                    if (isset($params['recommend_index'])) {
                        $v->addColumn('recommend_index', '推荐序号')->integer();
                    }
                    break;
                }
            case 'delActivity':
                {
                    $v->addColumn('act_id', '竞赛id')
                        ->required('不能为空')
                        ->integer()
                        ->min(0);
                    break;
                }
            case 'getActivity':
                {
                    $v->addColumn('act_id', '竞赛id')
                        ->required('不能为空')
                        ->integer();
                    break;
                }
            case 'getActivityList':
                {
                    $v->addColumn('recommend_status', '推荐状态')
                        ->required('不能为空')
                        ->integer();
                    $v->addColumn('limit', '列表数')
                        ->required('不能为空')
                        ->integer();
                    break;
                }
            case 'getPaper':
                {
                    $v->addColumn('act_id', '竞赛id')
                        ->required('不能为空')
                        ->integer();
                    $v->addColumn('paper_nums', '试卷题量')
                        ->required('不能为空')
                        ->integer();
                    break;
                }
            case 'addPaperTopic':
                {
                    $v->addColumn('act_id', '竞赛id')
                        ->required('不能为空')
                        ->integer();
                    $v->addColumn('topic_id', '题目id')
                        ->required('不能为空')
                        ->notEmpty();
                    $v->addColumn('topic_title', '题目标题')
                        ->required('不能为空')
                        ->notEmpty();
                    $v->addColumn('topic_label', '题目标签')
                        ->required('不能为空')
                        ->notEmpty();
                    break;
                }
            case 'delPaperTopic':
                {
                    $v->addColumn('act_id', '竞赛id')
                        ->required('不能为空')
                        ->integer();
                    $v->addColumn('topic_id', '题目id')
                        ->required('不能为空')
                        ->notEmpty();
                    break;
                }
            case 'updatePaper':
                {
                    $v->addColumn('act_id', '竞赛id')
                        ->required('不能为空')
                        ->integer();
                    $v->addColumn('type', '答题类型')
                        ->required('不能为空')
                        ->integer();
                    $v->addColumn('nums', '题量')
                        ->required('不能为空')
                        ->integer();
                    break;
                }
            case 'updateScoreSet':
                {
                    $v->addColumn('per_score', '答对得分')
                        ->required('不能为空')
                        ->integer();
                    $v->addColumn('win_score', '获胜得分')
                        ->required('不能为空')
                        ->integer();
                    $v->addColumn('fail_score', '失败得分')
                        ->required('不能为空')
                        ->integer();
                    break;
                }
        }
        return $v;
    }


App/HttpController/BaseController.php

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/10711/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2020-10-9 00:11
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]