{"id":1148,"date":"2017-08-04T09:05:35","date_gmt":"2017-08-04T16:05:35","guid":{"rendered":"http:\/\/mitchmckenna.com\/blog\/?p=1148"},"modified":"2017-08-07T14:26:58","modified_gmt":"2017-08-07T21:26:58","slug":"reduce-validation-bloat-in-laravel-controllers","status":"publish","type":"post","link":"http:\/\/mitchmckenna.com\/blog\/2017\/08\/reduce-validation-bloat-in-laravel-controllers\/","title":{"rendered":"Reduce Validation Bloat in Laravel Controllers"},"content":{"rendered":"<p>In Laravel or Lumen, I&#8217;ve often noticed that a significant portion of my controllers can be <a href=\"https:\/\/laravel.com\/docs\/5.4\/validation\">validation<\/a> calls. To solve this, I moved validation rules to a config file. By doing so, you can often simplify validation to just a single line of code.<\/p>\n<p>This allows you to go from this:<\/p>\n<pre><code>$this-&gt;validate($request, [\r\n    'title' =&gt; 'required|unique:posts|max:255',\r\n    'excerpt' =&gt; 'max:255',\r\n    'body' =&gt; 'required',\r\n    'category' =&gt; 'required',\r\n    'tags' =&gt; 'array',\r\n    'status' =&gt; 'required|in:draft,scheduled,published',\r\n    'publish_at' =&gt; 'nullable|date',\r\n]);\r\n<\/code><\/pre>\n<p>To just one line of code like this:<\/p>\n<p><code>$this-&gt;validate($request, config('validation.post.create'));<\/code><\/p>\n<h3>How To Pull Validation From A Config File<\/h3>\n<p>Create <code>\/config\/validation.php<\/code> and add your rules:<\/p>\n<pre><code>&lt;?php\r\nreturn [ \r\n    'post' =&gt; [\r\n        'create' =&gt; [\r\n            'title' =&gt; 'required|unique:posts|max:255',\r\n            'excerpt' =&gt; 'max:255',\r\n            'body' =&gt; 'required',\r\n            'category' =&gt; 'required',\r\n            'tags' =&gt; 'array',\r\n            'status' =&gt; 'required|in:draft,scheduled,published',\r\n            'publish_at' =&gt; 'nullable|date',\r\n        ]\r\n    ]\r\n];<\/code><\/pre>\n<p><i><b>Lumen:<\/b> You&#8217;ll need to load the config file manually. Open <code>\/bootstrap\/app.php<\/code> and after the line <code>$app-&gt;withEloquent();<\/code> add this: <code>$app-&gt;configure('validation');<\/code><\/i>.<\/p>\n<p>Enjoy.<\/p>\n<p style=\"text-align: center\">&hellip;<\/p>\n<blockquote><p>Wait, What about <a href=\"https:\/\/laravel.com\/docs\/5.4\/validation#form-request-validation\">form request<\/a> objects? They can be used to move validation bloat out of controllers too!<\/p><\/blockquote>\n<p>True, I&#8217;m a big fan of how form requests move validation and data transformation to a separate step before controller execution. However, I think you&#8217;ll find having all these different request objects (often just for the purpose of returning validation rules), can be overkill when you could just pull it from a config file. Not to mention, <a href=\"https:\/\/lumen.laravel.com\/docs\/5.4\/validation\">Lumen doesn&#8217;t support form requests<\/a>, so this is a great alternative if you&#8217;re using Lumen.<\/p>\n<p><a href=\"https:\/\/twitter.com\/mitchellmckenna\">Follow me<\/a> on Twitter, or subscribe to <a href=\"\/blog\/category\/programming\/laravel\/feed\/\">Laravel<\/a>\/<a href=\"\/blog\/category\/programming\/feed\/\">Programming<\/a> for future posts.<\/p>\n","protected":false},"excerpt":{"rendered":"<a href=\"http:\/\/mitchmckenna.com\/blog\/2017\/08\/reduce-validation-bloat-in-laravel-controllers\/\" rel=\"bookmark\" title=\"Permalink to Reduce Validation Bloat in Laravel Controllers\"><p>In Laravel or Lumen, I&#8217;ve often noticed that a significant portion of my controllers can be <a href=\"https:\/\/laravel.com\/docs\/5.4\/validation\">validation<\/a> calls. To solve this, I moved validation rules to a config file. By doing so, you can often simplify validation to just a single line of code [\u2026]<\/p>\n<\/a>","protected":false},"author":1,"featured_media":1180,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[171,16],"tags":[172,170,173],"class_list":{"0":"post-1148","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-laravel","8":"category-programming","9":"tag-laravel","10":"tag-lumen","11":"tag-validation","12":"h-entry","13":"hentry"},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/mitchmckenna.com\/blog\/wp-content\/uploads\/2017\/08\/laravel_logo.png?fit=800%2C600","jetpack_shortlink":"https:\/\/wp.me\/p92wZA-iw","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/mitchmckenna.com\/blog\/wp-json\/wp\/v2\/posts\/1148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mitchmckenna.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mitchmckenna.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mitchmckenna.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/mitchmckenna.com\/blog\/wp-json\/wp\/v2\/comments?post=1148"}],"version-history":[{"count":29,"href":"http:\/\/mitchmckenna.com\/blog\/wp-json\/wp\/v2\/posts\/1148\/revisions"}],"predecessor-version":[{"id":1187,"href":"http:\/\/mitchmckenna.com\/blog\/wp-json\/wp\/v2\/posts\/1148\/revisions\/1187"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/mitchmckenna.com\/blog\/wp-json\/wp\/v2\/media\/1180"}],"wp:attachment":[{"href":"http:\/\/mitchmckenna.com\/blog\/wp-json\/wp\/v2\/media?parent=1148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mitchmckenna.com\/blog\/wp-json\/wp\/v2\/categories?post=1148"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mitchmckenna.com\/blog\/wp-json\/wp\/v2\/tags?post=1148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}