F
Filament3mo ago
Dima

How to make filament to trim form input?

Laravel uses TrimStrings middleware to automatically trim all form string inputs. However, I noticed that filament allows data to be saved with trailing spaces. How do I enable filament to trim form inputs?
Solution:
```php // its by design from Livewire to skip TrimStrings // in the source code here vendor/livewire/livewire/src/Mechanisms/HandleRequests/HandleRequests.php // in the boot method they have...
Jump to solution
2 Replies
Solution
hyperion-mx
hyperion-mx3mo ago
// its by design from Livewire to skip TrimStrings
// in the source code here
vendor/livewire/livewire/src/Mechanisms/HandleRequests/HandleRequests.php
// in the boot method they have
$this->skipRequestPayloadTamperingMiddleware();

// which does exactly this
function skipRequestPayloadTamperingMiddleware()
{
.....
// as you can see it skip it
\Illuminate\Foundation\Http\Middleware\TrimStrings::skipWhen(function () {
return $this->isLivewireRequest();
});
}
// its by design from Livewire to skip TrimStrings
// in the source code here
vendor/livewire/livewire/src/Mechanisms/HandleRequests/HandleRequests.php
// in the boot method they have
$this->skipRequestPayloadTamperingMiddleware();

// which does exactly this
function skipRequestPayloadTamperingMiddleware()
{
.....
// as you can see it skip it
\Illuminate\Foundation\Http\Middleware\TrimStrings::skipWhen(function () {
return $this->isLivewireRequest();
});
}
hyperion-mx
hyperion-mx3mo ago
You will have to handle it outside Filament/Livewire, for example you can use the method configureUsing on the field for example
TextInput::configureUsing(function (TextInput $input) {
$input->mutateDehydratedStateUsing(function ($state) {
return Str::trim($state);
});
});
TextInput::configureUsing(function (TextInput $input) {
$input->mutateDehydratedStateUsing(function ($state) {
return Str::trim($state);
});
});
Which you use in your AppServiceProvider boot method and same applies to the rest of the fields like TextArea etc hope that helps

Did you find this page helpful?