{"id":11839,"date":"2022-03-18T10:00:00","date_gmt":"2022-03-18T01:00:00","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=11839"},"modified":"2022-03-17T21:02:17","modified_gmt":"2022-03-17T12:02:17","slug":"laravel%e3%81%ae%e3%83%95%e3%82%a9%e3%83%bc%e3%83%a0%e3%83%aa%e3%82%af%e3%82%a8%e3%82%b9%e3%83%88-prepareforvalidation","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/11839","title":{"rendered":"Laravel\u306e\u30d5\u30a9\u30fc\u30e0\u30ea\u30af\u30a8\u30b9\u30c8: prepareForValidation"},"content":{"rendered":"\n<p>\u4eca\u56de\u306f\u3001Laravel\u3067\u691c\u8a3c\u3092\u884c\u3046\u524d\u306b\u30ea\u30af\u30a8\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u64cd\u4f5c\u3059\u308b\u3053\u3068\u306b\u3064\u3044\u3066\u8a71\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h3>\u30d5\u30a9\u30fc\u30e0\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u4f8b<\/h3>\n\n\n\n<p>\u307e\u305a\u3001\u8a18\u4e8b\u306e\u6295\u7a3f\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306e\u30d5\u30a9\u30fc\u30e0\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u4f8b\u304b\u3089\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u5165\u529b\u306b\u9069\u7528\u3055\u308c\u308b\u30eb\u30fc\u30eb\u306b\u6ce8\u76ee\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace App\\Http\\Requests;\n\nuse Illuminate\\Foundation\\Http\\FormRequest;\n\nclass StoreRequest extends FormRequest\n{\n    \/**\n     * Get the validation rules that apply to the request.\n     *\n     * @return array\n     *\/\n    public function rules()\n    {\n        return &#091;\n            'title' =&gt; 'required|max:200',\n            'body' =&gt; 'required',\n            'tags' =&gt; 'required|array|max:10',\n            'is_published' =&gt; 'required|boolean',\n            'author_name' =&gt; 'required',\n        ];\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u3053\u3053\u3067\u3001\u30e6\u30fc\u30b6\u30fc\u304c\u6295\u7a3f\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306b\u30d5\u30a9\u30fc\u30e0\u3092\u9001\u4fe1\u3057\u3001\u6b21\u306e\u3088\u3046\u306a\u5165\u529b\u30c7\u30fc\u30bf\u3092\u6e21\u3057\u305f\u3068\u4eee\u5b9a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;\n    'title' =&gt; 'My  articles post',\n    'body' =&gt; 'This is the &lt;script&gt;alert('Alert!')&lt;\/script&gt; articles body.',\n    'tags' =&gt; 'laravel,code,updates',\n    'is_published' =&gt; 'true',\n    'author_name' =&gt; 'Ami',\n]<\/code><\/pre>\n\n\n\n<p>tags\u306f\u30ab\u30f3\u30de\u533a\u5207\u308a\u306e\u5024\u306e\u30ea\u30b9\u30c8\u3068\u3057\u3066\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u304b\u3089\u9001\u4fe1\u3055\u308c\u3001is_published\u306e\u5165\u529b\u306f\u6587\u5b57\u5217\u3068\u3057\u3066\u89e3\u91c8\u3055\u308c\u307e\u3059\u3002\u3069\u3061\u3089\u3082\u3001\u30b3\u30f3\u30c6\u30f3\u30c4\u306e\u89b3\u70b9\u304b\u3089\u306f\u6b63\u3057\u3044\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u5f62\u5f0f\u304c\u6b63\u3057\u304f\u306a\u3044\u3060\u3051\u3067\u691c\u8a3c\u306f\u5931\u6557\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3055\u3089\u306b\u3001title\u306b\u30bf\u30a4\u30d7\u30df\u30b9\u304c\u3042\u308a\u3001\u672c\u6587\u306b\u60aa\u610f\u306e\u3042\u308b\u30b3\u30fc\u30c9\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u3002\u6280\u8853\u7684\u306b\u306f\u3053\u308c\u3089\u306f\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306e\u554f\u984c\u3067\u306f\u306a\u3044\u3068\u8a00\u3048\u307e\u3059\u304c\u3001\u30e6\u30fc\u30b6\u30fc\u3092\u81ea\u5206\u81ea\u8eab\u304b\u3089\u6551\u3044\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4fdd\u8b77\u3059\u308b\u624b\u52a9\u3051\u3092\u3059\u308b\u3053\u3068\u306f\u53ef\u80fd\u3067\u3059\u3002<\/p>\n\n\n\n<h3><code>prepareForValidation<\/code><\/h3>\n\n\n\n<p>\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306fprepareForValidation\u3068\u3044\u3046\u9069\u5207\u306a\u540d\u524d\u3067\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u30a2\u30af\u30b7\u30e7\u30f3\u3092\u6301\u3061\u307e\u305b\u3093\u3002\u3064\u307e\u308a\u3001\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3059\u308b\u305f\u3081\u3060\u3051\u306b\u7f6e\u304b\u308c\u305f\u3082\u306e\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>protected function prepareForValidation()\n{\n    \/\/ no default action\n}<\/code><\/pre>\n\n\n\n<p>\u30d9\u30fc\u30b9\u3068\u306a\u308b FormRequest \u30af\u30e9\u30b9\u306f Request \u30af\u30e9\u30b9\u3092\u7d99\u627f\u3057\u3066\u3044\u308b\u306e\u3067\u3001 merge \u30d8\u30eb\u30d1\u30fc\u30e1\u30bd\u30c3\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001\u5fc5\u8981\u306a\u5165\u529b\u5024\u3060\u3051\u3092\u66f4\u65b0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u307e\u305f\u3001\u5165\u529b\u5024\u305d\u306e\u3082\u306e\u306b\u30af\u30e9\u30b9\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u306e\u3088\u3046\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>protected function prepareForValidation()\n    {\n        $this-&gt;merge(&#091;\n            'title' =&gt; fix_typos($this-&gt;title),\n            'body' =&gt; filter_malicious_content($this-&gt;body),\n            'tags' =&gt; convert_comma_separated_values_to_array($this-&gt;tags),\n            'is_published' =&gt; (bool) $this-&gt;is_published,\n        ]);\n    }<\/code><\/pre>\n\n\n\n<p>\u5024\u306f\u30ea\u30af\u30a8\u30b9\u30c8\u81ea\u4f53\u3067\u66f4\u65b0\u3055\u308c\u308b\u306e\u3067\u3001\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3092\u884c\u3063\u305f\u5f8c\u306b\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3067\u3069\u306e\u3088\u3046\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u3082\u3001\u64cd\u4f5c\u3055\u308c\u305f\u5024\u304c\u623b\u3063\u3066\u304f\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;\n    'title' =&gt; 'My blog post',                         \/\/ \u8aa4\u5b57\u3092\u4fee\u6b63\u3057\u307e\u3057\u305f!\n    'body' =&gt; 'This is the post body.',         \/\/\u60aa\u8cea\u306a\u30b3\u30f3\u30c6\u30f3\u30c4\u306f\u524a\u9664\u3055\u308c\u307e\u3057\u305f!\n    'tags' =&gt; &#091;'laravel', 'code', 'updates'],   \/\/ \u914d\u5217\u306b\u306a\u308a\u307e\u3057\u305f!\n    'is_published' =&gt; true,                     \/\/ \u4eca\u306f\u30d6\u30fc\u30eb\u5024\u3067\u3059\uff01\n    'author_name' =&gt; 'Ami',    \/\/ \u76f8\u5909\u308f\u3089\u305a\u3067\u3059\u3002\n<\/code><\/pre>\n\n\n\n<p>\u4eca\u56de\u306f\u3068\u3044\u3046\u3053\u3068\u3067\u7d42\u308f\u308a\u307e\u3059\u3002<\/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\/11839\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/11839\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Laravel\u306e\u30d5\u30a9\u30fc\u30e0\u30ea\u30af\u30a8\u30b9\u30c8: prepareForValidation\" 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\/11839\" data-text=\"Laravel\u306e\u30d5\u30a9\u30fc\u30e0\u30ea\u30af\u30a8\u30b9\u30c8: prepareForValidation\" 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\/11839\" 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\/11839\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u56de\u306f\u3001Laravel\u3067\u691c\u8a3c\u3092\u884c\u3046\u524d\u306b\u30ea\u30af\u30a8\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u64cd\u4f5c\u3059\u308b\u3053\u3068\u306b\u3064\u3044\u3066\u8a71\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 \u30d5\u30a9\u30fc\u30e0\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u4f8b \u307e\u305a\u3001\u8a18\u4e8b\u306e\u6295\u7a3f\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306e\u30d5\u30a9\u30fc\u30e0\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u4f8b\u304b\u3089\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u5165\u529b\u306b\u9069\u7528\u3055\u308c\u308b\u30eb\u30fc\u30eb\u306b [&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\/11839"}],"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=11839"}],"version-history":[{"count":5,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11839\/revisions"}],"predecessor-version":[{"id":11846,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11839\/revisions\/11846"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=11839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=11839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=11839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}