I have MyRequest.php
class extending App\Http\Requests\Request
. I want to trim()
every input before validation because an e-mail with a space after it does not pass validation.
However sanitize()
was removed from src/Illuminate/Foundation/Http/FormRequest.php
I just came across for the same problem.
I'd like to show you another way of doing it without
extends
but withtraits
. ( I will take the Example Classes from Tarek Adam ).PHP Traits are like functions which will be injected into the used class. The one main difference is that a Trait doesn't need any dependency like a extends do. This means you can use a trait for more then just one class e.x. for Controllers, Requests and whatever you like.
Laravel provides some traits in the BaseController, we can do the same.
How to do it with a trait
Create a trait as file in
\App\Traits\SanitizedRequest.php
. You can create it anywhere it doesn't matter really. You have to provide the correct namespace for sure.In your Request you can use the trait with
use SanitizedRequest
keyword.Create an abstract SanitizedRequest class that extends the usual Request class.
YourRequest class should extend your SanitizedRequest abstract class.
Your SanitizedRequest class overrides Request::all() as like so...
Then a normal CustomRequest, but extend SanitizedRequest instead of laravel's Request class