From fbfe238f1947e0ba1d5414601cee756ea2d0d20c Mon Sep 17 00:00:00 2001 From: joycezhang <787027175@qq.com> Date: Fri, 16 Sep 2022 16:30:28 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7:=20=E8=A1=A8=E5=8D=95=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Validation/BaseRequest.php | 53 +++++++++++++++++++++++++++ src/resources/stubs/request.stub | 61 ++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 src/Validation/BaseRequest.php create mode 100644 src/resources/stubs/request.stub diff --git a/src/Validation/BaseRequest.php b/src/Validation/BaseRequest.php new file mode 100644 index 0000000..ccd5b08 --- /dev/null +++ b/src/Validation/BaseRequest.php @@ -0,0 +1,53 @@ +route()->getActionMethod()); + + if (method_exists($this, $rule_action)) + return $this->$rule_action(); + + return $this->getDefaultRules(); + } + + /** + * 默认验证规则 + * @return array + */ + protected function getDefaultRules() + { + return []; + } + + /** + * 验证消息通过,json抛出,api开发 + * @param Validator $validator + * @throws \HttpResponseException + */ + protected function failedValidation(Validator $validator) + { + throw new HttpResponseException(response()->json([ + 'code' => -1, + 'message' => $validator->errors()->first() + ])); + } +} \ No newline at end of file diff --git a/src/resources/stubs/request.stub b/src/resources/stubs/request.stub new file mode 100644 index 0000000..6e4d1ab --- /dev/null +++ b/src/resources/stubs/request.stub @@ -0,0 +1,61 @@ +store( )的验证规则 + * @return array + */ + public function getRulesByStore() + { + return [ + + ]; + } + + /** + * 定义针对 Controller->update( )的验证规则 + * @return array + */ + public function getRulesByUpdate() + { + return [ + + ]; + } + + /** + * 定义针对 Controller->delete( )的验证规则 + * + * @return array + */ + public function getRulesByDelete() + { + return [ + ]; + } + + /** + * 统一定义验证规则的自定义错误消息。 + * @return array + */ + public function messages() + { + return [ + + ]; + } + + +}