{"id":25186,"date":"2024-11-21T11:01:15","date_gmt":"2024-11-21T03:01:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25186\/"},"modified":"2024-11-21T11:01:15","modified_gmt":"2024-11-21T03:01:15","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8laravel%e4%b8%ad%e4%bf%ae%e6%94%b9%e4%b8%ad%e6%96%87%e9%aa%8c%e8%af%81%e8%a7%84%e5%88%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25186\/","title":{"rendered":"\u5982\u4f55\u5728Laravel\u4e2d\u4fee\u6539\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/170977230556089.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728Laravel\u4e2d\u4fee\u6539\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728Laravel\u4e2d\u4fee\u6539\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219\u63d2\u56fe\" \/><\/p>\n<p>\u5728Laravel\u4e2d\u4fee\u6539\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219\u662f\u5f88\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u7279\u522b\u662f\u5728\u4e2d\u6587\u73af\u5883\u4e0b\u5f00\u53d1\u9879\u76ee\u65f6\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cLaravel\u7684\u9a8c\u8bc1\u89c4\u5219\u662f\u82f1\u6587\u7684\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668\u6765\u4fee\u6539\u4e3a\u4e2d\u6587\u89c4\u5219\uff0c\u8ba9\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u6613\u61c2\u3002\u4e0b\u9762\u5c06\u4ecb\u7ecd\u5177\u4f53\u7684\u6b65\u9aa4\uff0c\u5305\u62ec\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668<\/strong><\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668\uff0c\u4ee5\u4fbf\u5728\u5176\u4e2d\u5b9a\u4e49\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219\u3002\u5728Laravel\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Artisan\u547d\u4ee4\u751f\u6210\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668\uff1a<\/p>\n<pre>php artisan make:validator CustomValidator<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u5728app\/Validators\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2aCustomValidator.php\u6587\u4ef6\uff0c\u6211\u4eec\u5c06\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219\u3002<\/p>\n<ol>\n<li><strong>\u5b9a\u4e49\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219<\/strong><\/li>\n<\/ol>\n<p>\u5728CustomValidator.php\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e2d\u6587\u7684\u9a8c\u8bc1\u89c4\u5219\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>namespace AppValidators;\n\nuse IlluminateValidationValidator;\n\nclass CustomValidator extends Validator\n{\n    protected $customMessages = [\n        'required' =&amp;gt; '\u5fc5\u586b\u9879',\n        'email' =&amp;gt; '\u90ae\u7bb1\u683c\u5f0f\u4e0d\u6b63\u786e',\n        'numeric' =&amp;gt; '\u5fc5\u987b\u4e3a\u6570\u5b57',\n        \/\/ \u53ef\u6839\u636e\u9700\u8981\u6dfb\u52a0\u66f4\u591a\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219\n    ];\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528$customMessages\u6570\u7ec4\u6765\u5b9a\u4e49\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219\uff0c\u4f8b\u5982\u5c06&#8217;required&#8217;\u6539\u4e3a&#8217;\u5fc5\u586b\u9879&#8217;\uff0c&#8217;email&#8217;\u6539\u4e3a&#8217;\u90ae\u7bb1\u683c\u5f0f\u4e0d\u6b63\u786e&#8217;\u7b49\u3002<\/p>\n<ol>\n<li><strong>\u6ce8\u518c\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668<\/strong><\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5728AppServiceProvider\u7684boot\u65b9\u6cd5\u4e2d\u6ce8\u518c\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668\uff0c\u8ba9Laravel\u77e5\u9053\u6211\u4eec\u8981\u4f7f\u7528\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668\u3002\u5728AppServiceProvider.php\u6587\u4ef6\u4e2d\uff1a<\/p>\n<pre>namespace AppProviders;\n\nuse IlluminateSupportServiceProvider;\nuse AppValidatorsCustomValidator;\n\nclass AppServiceProvider extends ServiceProvider\n{\n    public function boot()\n    {\n        $this-&amp;gt;app['validator']-&amp;gt;resolver(function($translator, $data, $rules, $messages) {\n            return new CustomValidator($translator, $data, $rules, $messages);\n        });\n    }\n\n    public function register()\n    {\n        \/\/\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06CustomValidator\u6ce8\u518c\u5230Laravel\u4e2d\uff0c\u4ee5\u4fbf\u5728\u9a8c\u8bc1\u65f6\u4f7f\u7528\u6211\u4eec\u5b9a\u4e49\u7684\u4e2d\u6587\u89c4\u5219\u3002<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219<\/strong><\/li>\n<\/ol>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u63a7\u5236\u5668\u6216\u8005\u8868\u5355\u8bf7\u6c42\u4e2d\u76f4\u63a5\u4f7f\u7528\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219\uff1a<\/p>\n<pre>$request-&amp;gt;validate([\n    'email' =&amp;gt; 'required|email',\n    'password' =&amp;gt; 'required|min:6',\n], [\n    'email.required' =&amp;gt; '\u90ae\u7bb1\u4e3a\u5fc5\u586b\u9879',\n    'password.required' =&amp;gt; '\u5bc6\u7801\u4e3a\u5fc5\u586b\u9879',\n    'password.min' =&amp;gt; '\u5bc6\u7801\u957f\u5ea6\u4e0d\u80fd\u5c11\u4e8e6\u4e2a\u5b57\u7b26',\n]);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u5c31\u6210\u529f\u5730\u5728Laravel\u4e2d\u4fee\u6539\u4e86\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6613\u8bfb\u3001\u6613\u61c2\uff0c\u5728\u4e2d\u6587\u73af\u5883\u4e0b\u5f00\u53d1\u66f4\u52a0\u65b9\u4fbf\u3002\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Laravel\u4e2d\u4fee\u6539\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Laravel\u4e2d\u4fee\u6539\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219\u662f\u5f88\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u7279\u522b\u662f\u5728\u4e2d\u6587\u73af\u5883\u4e0b\u5f00\u53d1\u9879\u76ee\u65f6\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cLaravel\u7684\u9a8c\u8bc1\u89c4\u5219\u662f\u82f1\u6587\u7684\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668\u6765\u4fee\u6539\u4e3a\u4e2d\u6587\u89c4\u5219\uff0c\u8ba9\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u6613\u61c2\u3002\u4e0b\u9762\u5c06\u4ecb\u7ecd\u5177\u4f53\u7684\u6b65\u9aa4\uff0c\u5305\u62ec\u4ee3\u7801\u793a\u4f8b\uff1a \u521b\u5efa\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668\uff0c\u4ee5\u4fbf\u5728\u5176\u4e2d\u5b9a\u4e49\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219\u3002\u5728Laravel\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Artisan\u547d\u4ee4\u751f\u6210\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668\uff1a php artisan make:validator CustomValidator \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u5728app\/Validators\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2aCustomValidator.php\u6587\u4ef6\uff0c\u6211\u4eec\u5c06\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219\u3002 \u5b9a\u4e49\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219 \u5728CustomValidator.php\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e2d\u6587\u7684\u9a8c\u8bc1\u89c4\u5219\uff0c\u4f8b\u5982\uff1a namespace AppValidators; use IlluminateValidationValidator; class CustomValidator extends Validator { protected $customMessages = [ &#8216;required&#8217; =&amp;gt; &#8216;\u5fc5\u586b\u9879&#8217;, &#8217;email&#8217; =&amp;gt; &#8216;\u90ae\u7bb1\u683c\u5f0f\u4e0d\u6b63\u786e&#8217;, &#8216;numeric&#8217; =&amp;gt; &#8216;\u5fc5\u987b\u4e3a\u6570\u5b57&#8217;, \/\/ \u53ef\u6839\u636e\u9700\u8981\u6dfb\u52a0\u66f4\u591a\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219 ]; } \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528$customMessages\u6570\u7ec4\u6765\u5b9a\u4e49\u4e2d\u6587\u9a8c\u8bc1\u89c4\u5219\uff0c\u4f8b\u5982\u5c06&#8217;required&#8217;\u6539\u4e3a&#8217;\u5fc5\u586b\u9879&#8217;\uff0c&#8217;email&#8217;\u6539\u4e3a&#8217;\u90ae\u7bb1\u683c\u5f0f\u4e0d\u6b63\u786e&#8217;\u7b49\u3002 \u6ce8\u518c\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5728AppServiceProvider\u7684boot\u65b9\u6cd5\u4e2d\u6ce8\u518c\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668\uff0c\u8ba9Laravel\u77e5\u9053\u6211\u4eec\u8981\u4f7f\u7528\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668\u3002\u5728AppServiceProvider.php\u6587\u4ef6\u4e2d\uff1a namespace AppProviders; use IlluminateSupportServiceProvider; use AppValidatorsCustomValidator; class AppServiceProvider extends ServiceProvider { public function boot() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-25186","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=25186"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25186\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}