//接收参数 $params = $request->param(); //参数验证 try { validate([ 'id' => 'require|integer|gt:0', 'article_ids' => 'require|array|min:1|max:' . $this->pagesize ], [ 'article_ids' => "文章id数组" ])->check($params); } catch (ValidateException $e) { //表单验证不通过 //这里根据业务处理 return fail($e->getError()); }
控制器继承\app\BaseController
//参数验证 try { $this->validate($params, [ 'name|专栏名称' => 'require|max:30|unique:column,user_id=' . $this->user_id, 'desc|专栏描述' => 'require|max:150', 'sort|排序' => 'require|integer|>=:0', 'visible|前台显示' => 'require|in:1,-1', ]); } catch (ValidateException $e) { //表单验证不通过 //根据业务处理 //跳转回提交页面 return redirect((string)url('/console/column/create'))->with(array_merge(['error' => $e->getError()], $params)); }