LoginSignup
3
1

More than 3 years have passed since last update.

入力フィールドの自動トリミングをoffにする方法(Laravel)

Last updated at Posted at 2021-01-30

はじめに

Laravelではミドルウェアで、全てのフィールドに自動的にトリム処理が走ります。(文字列の前後にスペースがあっても自動的に削除してくれる)
今回は、その設定をoffにする方法を書きます✍️

結論

・全てのフィールドのトリム処理を無効にする方法

ミドルウェアの記述をコメントアウトする

app/Http/Kernel.php
Kernel.php
    protected $middleware = [
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        // \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,
    ];

App\Http\Middleware\TrimStringsがトリム処理のミドルウェアとなりますので、上記のようにコメントアウトしてあげれば全てのフィールドのトリムを無効にすることができます。

・特定のフィールドのトリム処理を無効にする方法

また、$exceptプロパティに除外したいフィールドを設定することで、特定のフィールドのみトリム処理を無効にすることもできます。

app/Http/Middleware/TrimStrings.php
TrimStrings.php
    protected $except = [
        'password',
        'password_confirmation',
        'hoge',// 除外したいフィールド名を記述してあげる
    ];

上記のように記述してください🙇‍♂️
はい、以上となります!

おわりに

見てくださった方ありがとうございます!
もし誤字・脱字等ありましたらご指摘頂けると嬉しいです:writing_hand_tone1:
よろしくお願いいたします。


参考文献

入力のトリムと正規化

3
1
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
3
1