LoginSignup
0
2

More than 1 year has passed since last update.

Laravel mimetypesでバリデーション設定したら突破するはずの値が弾かれる

Posted at

目的

  • 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',
                ],
            ];
        }
    }
    
  • 上記のように半角スペースを開けずに記載することでバリデーションを突破する事ができた。

参考文献

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2