{"id":178,"date":"2023-02-19T19:17:52","date_gmt":"2023-02-19T11:17:52","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=178"},"modified":"2023-02-19T19:18:27","modified_gmt":"2023-02-19T11:18:27","slug":"laravel-validator-extension","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/19\/laravel-validator-extension\/","title":{"rendered":"Laravel\u4e2dValidator\u7684\u9a8c\u8bc1\u6269\u5c55"},"content":{"rendered":"<p>\u867d\u7136Laravel\u63d0\u4f9b\u4e00\u5957\u5305\u62ec\u6b63\u5219\u5339\u914d\u5728\u5185\u7684\u5b8c\u5584\u7684\u9a8c\u8bc1\u673a\u5236\uff0c\u4f46\u4f60\u4e00\u5b9a\u4f1a\u9047\u5230\u4f7f\u7528\u89c4\u5219\u89e3\u51b3\u4e0d\u4e86\u7684\u9a8c\u8bc1\uff0c\u6bd4\u5982\u6570\u636e\u5e93\u9a8c\u8bc1\uff0c\u8fd9\u65f6\u9700\u8981\u7528\u5230Validator\u7684\u9a8c\u8bc1\u6269\u5c55\u3002<\/p>\n<p>\uff081\uff09\u4f7f\u7528\u9759\u6001\u65b9\u6cd5\uff1aValidator::extend<br \/>\n\uff082\uff09\u4f7f\u7528\u5bf9\u8c61\u8c03\u7528\uff1a$validator-&gt;addExtension<\/p>\n<p><!-- more --><\/p>\n<p>\u4e0b\u9762\u662f\u9a8c\u8bc1HTTP\u63d0\u4ea4\u7684\u5546\u54c1\u5206\u7c7b\u4fe1\u606f\u7684\u4f8b\u5b50\u3002<\/p>\n<pre><code class=\"language-php\">use \\Illuminate\\Http\\Request;\nuse App\\Exceptions\\MyValidationException;\nuse App\\GoodsCategory;\n\nclass GoodsValidator\n{\n\n    public static function validate(Request $request, $final = false)\n    {\n        $messages = [\n            \/\/&#039;category_id.required&#039; =&gt; &#039;:attribute \u8f93\u5165\u4e0d\u6b63\u786e\uff01&#039;,\n            &#039;category_id.required&#039; =&gt; &#039;\u62b1\u6b49\uff0c\u60a8\u8f93\u5165\u7684\u5546\u54c1\u5206\u7c7b\u4fe1\u606f\u4e3a\u7a7a&#039;,\n            &#039;category_id.min&#039; =&gt; &#039;\u62b1\u6b49\uff0c\u60a8\u8f93\u5165\u7684\u5546\u54c1\u5206\u7c7b\u4fe1\u606f\u957f\u5ea6\u6709\u8bef&#039;,\n            &#039;category_id.category_id_check&#039; =&gt; &#039;\u62b1\u6b49\uff0c\u60a8\u8f93\u5165\u7684\u5546\u54c1\u5206\u7c7b\u4fe1\u606f\u4e0d\u5b58\u5728&#039;,\n        ];\n\n        $rules = [\n            &#039;category_id&#039; =&gt; &#039;required|min:4|category_id_check&#039;,\n        ];\n\n        Validator::extend(&#039;category_id_check&#039;, function($attribute, $value, $parameters) {\n            return GoodsCategory::where(&#039;category_id&#039;, $value)-&gt;exists();\n        });\n\n        $validator = Validator::make($request-&gt;all(), $rules, $messages);\n        \/*\n        $validator-&gt;addExtension(&#039;category_id_check&#039;, function($attribute, $value, $parameters) {\n            return GoodsCategory::where(&#039;category_id&#039;, $value)-&gt;exists();\n        });\n        *\/\n        if ($validator-&gt;fails()) {\n            $messageBag = $validator-&gt;messages();\n            $messageArr = $messageBag-&gt;getMessages();\n            print_r($messageArr);\n        }\n    }\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u867d\u7136Laravel\u63d0\u4f9b\u4e00\u5957\u5305\u62ec\u6b63\u5219\u5339\u914d\u5728\u5185\u7684\u5b8c\u5584\u7684\u9a8c\u8bc1\u673a\u5236\uff0c\u4f46\u4f60\u4e00\u5b9a\u4f1a\u9047\u5230\u4f7f\u7528\u89c4\u5219\u89e3\u51b3\u4e0d\u4e86\u7684\u9a8c\u8bc1\uff0c\u6bd4\u5982\u6570\u636e\u5e93\u9a8c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-178","post","type-post","status-publish","format-standard","hentry","category-laravel"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=178"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/178\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}