目的
- mimetypesのバリデーションルールを設定後にバリデーションを通過するはずの値が弾かれる問題の原因をまとめる
原因
- Mimetypeの指定時にスペースを入れてしまっていた。
問題のソース
-
下記のようにバリデーションルールを記載した。
アプリ名ディレクトリ/todos/app/Http/Requests/TaskRequest.php<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class TaskRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'name' => ['required'], 'limit_id' => ['required'], 'file' => [ 'required', 'file', 'image', 'mimetypes: image/jpeg, image/png', ], ]; } }
-
注目いただきたいソースは
'mimetypes: image/jpeg, image/png',
の部分であり、Mimetype名の前に半角スペースが空いている。これが原因だった。
問題を解消したソース
-
下記のようにバリデーションルールを記載した。
アプリ名ディレクトリ/todos/app/Http/Requests/TaskRequest.php<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class TaskRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'name' => ['required'], 'limit_id' => ['required'], 'file' => [ 'required', 'file', 'image', 'mimetypes:image/jpeg,image/png', ], ]; } }
-
上記のように半角スペースを開けずに記載することでバリデーションを突破する事ができた。