{"id":9808,"date":"2021-04-09T10:00:12","date_gmt":"2021-04-09T01:00:12","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9808"},"modified":"2021-04-08T19:09:17","modified_gmt":"2021-04-08T10:09:17","slug":"prohibited-validation-rules-in-laravel","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9808","title":{"rendered":"Prohibited Validation Rules in Laravel"},"content":{"rendered":"\n<p>Sometimes we wish to forcefully prevent some of the fields in the request. <\/p>\n\n\n\n<p>Now we just got that. Laravel 8 has three validation added for prohibited fields. Let\u2019s walk through a few examples of where the prohibited validation rules might be useful and look at each one in more detail.<\/p>\n\n\n\n<p><strong>Prohibited Validation Rule<\/strong><\/p>\n\n\n\n<p>The  <strong>\u201cprohibited<\/strong>\u201d  rule checks if the field under validation must be empty or not present or throw an error otherwise.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ PUT \/api\/licenses\/123-456\n\n\/\/ {\"name\":\"hello-world\", \"key\":\"random-key\"}\n\n$validated = $request->validate([\n    'name' => 'required|max:255',\n    'key' => 'prohibited',\n]);\n\t\t\t\n\/\/ Response: 422\n\n\/\/ The key field is prohibited\n<\/code><\/pre>\n\n\n\n<p>The above is where a user might expect to update an API key by sending a PUT request to a resource. That field is likely ignored during the request. However, a successful response might lead to the user to believe they were able to update the <strong>key <\/strong>when in reality, the API ignored it. <\/p>\n\n\n\n<p>If&nbsp; prohibited validation rule registers, the <strong>key <\/strong>field is present in the request it will terminate the request with 422 HTTP code response code&nbsp; and throw the error with the message <strong>The key field is prohibited.<\/strong><\/p>\n\n\n\n<p><strong>Prohibited If and Unless Rules<\/strong><\/p>\n\n\n\n<p>The  <strong>\u201cprohibited_if \u201d <\/strong> rule checks if the field under validation must be empty or not present if the another field is equal to any value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Validator::validate([\n\u2018is_minor\u2019 => true,\n\u2018tos_accepted\u2019 => true\n], [\n\u2018tos_accepted\u2019 => \u2018prohibited_if:is_minor, true\u2019\n]);<\/code><\/pre>\n\n\n\n<p>The above example is we\u2019re expecting tos_accepted field will be forbidden if is_minor filed accepts true value. This example means someone accepting terms of service that has identified as a minor. Perhaps the application requires a parental registration to consent on their behalf.<\/p>\n\n\n\n<p>The basic idea of  <strong>\u201cProhibited_unless\u201d<\/strong> rule is that a given filed should be prohibited from having data unless another field is equal to any value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Validator::validate([\n\u2018is_deceased\u2019 => false,\n\u2018date_of_death\u2019 => \u20182021-03-09\u2019\n], [\n\u2018date_of_death\u2019 => \u2018prohibited_unless:is_deceased,true\u2019\n]);<\/code><\/pre>\n\n\n\n<p>The above example might be initially the is_deceased filed set to false value.If we\u2019re expecting the date_of_death field not to be forbidden, the is_deceased\u2019 field set to true value.The example&nbsp; illustrates perfectly how to use this rule to&nbsp;<em>explicitly<\/em> prevent contradictory input.<\/p>\n\n\n\n<p>By Ami<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9808\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9808\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Prohibited Validation Rules in Laravel\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9808\" data-text=\"Prohibited Validation Rules in Laravel\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9808\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9808\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes we wish to forcefully prevent some of the fields in the request. Now we just got that. Laravel 8 has [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9808"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=9808"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9808\/revisions"}],"predecessor-version":[{"id":9811,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9808\/revisions\/9811"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}